2012-01-03 8 views
0

2番目のwhileループは、2番目の繰り返しでxstring行1441に例外をスローします。これは、最初のパスでxstringの行番号1331の文字列添え字

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

がすべてデバッグcoutステートメントから検証されたためです。私はこの1つに困惑しています。

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を作成することを意味する:

string str1(n1, ' '); 

または、おそらくあなたはコピー時に文字列データを割り当てることを意味:

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

あなたの中に他のエラーがあります。あなたが記述した例外がこのコードで生成される可能性があります。

これらのエラーはすべて単純に回避することができます。内側のwhileループを次のように置き換えます。

str1 = Testthis->NAME; 
+0

ありがとうございます。最後に私がプログラミングをしたのは98年でした – John

+0

この 'str1 = Testthis-> NAME;'は変換エラーです。これらのデータ型に互換性がある場合、char [64]をstd :: string [64]に変換できません。 – John

+0

Hmm。投稿したコードでは、 'str1'を' std :: string str1; 'と宣言しました。これを 'std :: string str1 [64]'に変更しましたか?最初のケースでは、タイプは互換性があります。第二に、彼らはそうではありません。 –

関連する問題