C++プログラムで複数のクラスを処理しようとするのはこれが初めてで、コードをじっと見つめて何が悪いのかを理解しようとするだけで何十時間も費やしたので、経験は本当にひどいものでした. また、私の実際のコードは現在約 600 行なので、最も気になる部分に関連するコードを掲載して説明します。
ノードと呼ばれる多方向検索ツリーのクラスがあります。そして、私の目的は、O(ログ n) 時間。このコードフラグメントを考えてみましょう
//Basically searches for a string in Avl tree with root t and returns pointer
//to the corresponding node in the multi-way tree
node* Avlnode::Avlsearch(string x,Avlnode* t)
{
if (t==NULL)
{
cout<<"why you search whats not in string";//invalid search
exit(0);
}
else
{
if(isLess(x,t->element)) //isLess compares strings and works good
{ //element refers to the string stored in Avlnode
Avlsearch(x,t->left);
}
else if (isLess(t->element,x))
{
Avlsearch(x,t->right);
}
else
{
cout<<"found";
cout<<t->index->empname;
return t->index; //its fine till here
}
}
}
//basically adds child under the boss in multi-way tree, by searching for the
//address of boss first using the AVL tree
void node::insertunderboss(string child, string boss,Avlnode* obj1)
{
node* temp=obj1->Avlsearch(boss, obj1->root); //root=root of Avltree
//why is address of temp and t->index not the same??
cout<<temp;
cout<<"root current is "<<obj1->root->element;
cout<<"\n"<<temp->empname; //empname is string stored in node
//this refuses to work even though t->index->empname worked perfect.
// in my first function t->index->empname gave me a string
// I just stored t->index inside temp
//But temp->empname makes cmd hang. Your program has stopped working.
node* c=insertemp(child,temp);
cout<<"last check";
obj1->Avlinsert(c,obj1->root);
return;
}
私の質問が明確だったことを願っています。参照渡しを考えましたが、なぜこれが機能しないのかわかりません。論理的には問題ないようです。どんな種類の助けも本当に感謝しています。