2012-01-03 3 views
0

두 번째 while 루프는 두 번째 반복에서 xstring 줄 1441에 예외를 던지고 있습니다. 이 때문에 첫 번째 패스에 : xstring 줄 1331의 문자열 첨자가

Testthis->NAME[n] = 'B' 
Testthis->NAME[n+1] = 'O' 
Testthis->NAME = "BOD MQL" 

모든 디버그 cout 문에서 확인됩니다. 나는이 하나에 난처하게된다.

struct Node * ProbableMatch(struct Node * Look_in, int MaxNodes, char Findthis[64]){ 
system("cls"); 
int i=0,proba=100,CurrentHigh=100; 
size_t pos1=1,n1=0,n2=0,pos2=1; 
//char s[64]; 
struct Node * CurrentHighProb; 
struct Node * Testthis; 
CurrentHighProb=new(Node); 
Testthis=new(Node); 
Testthis=Look_in; 
//for(i=0;i==MaxNodes;i++) 
while(!Testthis || i!=ccounter) 
{ 
    gotoxy(0,0); 
    int n=0; 
    n1=sizeof(Look_in->NAME); 
    n2=sizeof(Findthis); 
    string str1; 
    char str2[64]; 
    cout<<Testthis->NAME[n]<<endl<<Testthis->NAME<<endl; 
    cout<<Testthis->NAME[n+1]<<endl; 

    while(Testthis->NAME[n]!='\0'){ //mannually coverts the strings since I am having hell with the inbuilt functions 
     cout<<Testthis->NAME[n]; 
     str1[n]=Testthis->NAME[n]; 
     if(Testthis->NAME[n+1]=='\0'){str1[n+1]='\0';}//makes NULL terminated strings 
     n++; 
    }//end of while 

    //strcpy_s(cstr, str1.length()); 
    //strcpy_s(str1,sizeof(Look_in->NAME),Look_in->NAME); 
    //strcpy_s(str2,sizeof(Findthis),Findthis); 
    //char * cstr1 = new char[str1.length()+1]; 
    cout<<str1.compare(pos1,n1,Findthis,0,n2)<<" is the value of the string compare "<<endl; 
    proba=str1.compare(pos1,n1,Findthis,0,n2);//compares Findthis to the varibles read from the database from string matches 

    //DEBUG STUFF 
    cout<<endl<<sizeof(Look_in->NAME)<<" sizeof(Look_in->NAME)"<<Look_in->NAME<<endl; 
    cout<<sizeof(Findthis)<<" sizeof(Findthis)"<<Findthis<<endl; 
    cout<<sizeof(str1)<<" "<<str1<<" string1 string2 "<<str2<<" "<<sizeof(str2)<<endl; 
    //cout<<sizeof(str1)<<" reinterpret_cast< char *>(str1)"<<endl; 
    system("PAUSE"); 
    cout<<"\n Probability of "<<Look_in->NAME<<" matching "<<Findthis<<" is "<<proba<<" ."<<endl; 
    //ENDOFDEBUG FOR FUNCTION 
    Testthis->prob=proba; 
    if(proba==0) 
    { 
     cout<<"proba equals zero, match found returning "<<Look_in<<endl; 
     //delete[] cstr1; 
     return Testthis; // returns the pointer of the varible that best matched the search 
    } 
    if(proba<CurrentHigh){ 
     CurrentHigh=proba; 
     CurrentHighProb=Testthis; 
    } 
    i++; 
    Testthis=Testthis->next; 
} 
cout<<"Perfect match not found after "<<i<<" number of searches, returning this highest probable match "<<CurrentHighProb<<" "<<proba<<endl; 

system("PAUSE"); 
//delete[] cstr1; 
return CurrentHighProb; 
} 
+0

'Testthis = new (Node); Testthis = Look_in;'안녕하세요, 메모리 누수가 있습니다. 올바른 C++ 코드를 작성하는 방법을 배우려면 [좋은 입문 C++ 책] (http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list)을 얻는 것을 고려하십시오. –

+0

고마워요.하지만이 시점에서 저는 올바른 항목을 찾기 위해 함수를 얻으려고합니다. 프로그램이 끝나면 머리 포인터를 전달하는 것으로 전환 할 것입니다. – John

답변

1
string str1; 

는 길이 제로의 std::string을 선언합니다.

str1[n]=Testthis->NAME[n]; 

str1의 (존재하지 않는) n 번째 요소를 참조합니다. 아마도 당신은 복사 중에 문자열 데이터를 할당하는 것을 의미,

string str1(n1, ' '); 

을 또는 :

아마 당신은 확실한 길이와 string를 만드는 의미

str1.push_back(Testthis->NAME[n]); 

는 다른 오류가 당신의 코드,하지만이 하나의 가능성이 당신이 설명하는 예외를 생산.

이러한 오류를 모두 피할 수 있습니다. 내부 while 루프를 다음으로 교체하십시오.

str1 = Testthis->NAME; 
+0

감사합니다. 마지막으로 프로그래밍을 한 시간이 98 ' – John

+0

이'str1 = Testthis-> NAME;'은 변환 오류가 발생합니다. char [64]를 std :: string [64]로 변환 할 수 없습니까? – John

+0

흠. 게시 한 코드에서'str1'을'std :: string str1;'으로 선언했습니다. 이것을 std :: string str1 [64]'로 변경 했습니까? 첫 번째 경우에는 형식이 호환됩니다. 둘째로, 그들은 그렇지 않습니다. –