C++ : 在处理多个类时感到困惑
C++ : Confused in dealing with multiple classes
这是我第一次尝试在 c++ 程序中处理多个类,这种体验真的很糟糕,因为我花了几十个小时盯着代码并试图找出问题所在。 此外,我目前的实际代码大约有 600 行,所以我只会把相关的代码放在最困扰我的地方并尝试解释它。
我有一个叫做node的多路搜索树的类。我的目标是使用 AVL 树(类名 Avlnode)作为索引树(包含每个 Avlnode 上相应节点的排序顺序和地址的单词),以便在 O(log 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;
}
我希望我的问题很清楚。我想通过引用,但我无法弄清楚为什么这不起作用,从逻辑上讲它看起来没问题。任何帮助将不胜感激。
看起来您缺少返回语句,这是一个改进
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
return Avlsearch(x,t->left);
^^^^^^
}
else if (isLess(t->element,x))
{
return Avlsearch(x,t->right);
^^^^^^
}
else
{
cout<<"found";
cout<<t->index->empname;
return t->index; //its fine till here
}
}
}
相关文章:
- 警告处理为错误这里有什么问题
- 在C#中处理C++指针而不使用unsafe的最佳方法
- 处理多个异常集合的C++方法
- 找不到成员对象:没有名为get_event()的成员,也处理多态性和向量
- 使用流处理接收到的数据
- 获取日期异步信号安全吗?如果在信号处理程序中使用,它会导致死锁吗
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- 基于多个条件处理地图中的所有元素
- 如何用数字处理log(0)
- SSL上的`curl_easy_send`和`curl_asy_recv`:如何处理`CURLE_AGAIN`
- 错误处理.将系统错误代码映射到泛型
- 从文本文件中读取时钟时间和事件时间并进行处理
- 在运行时处理类型擦除的数据-如何不重新发明轮子
- 在for循环中使用auto vs decltype(vec.size())来处理字符串的向量
- 用于矢量处理的多个线程
- 对字符串进行排序时,在c++中处理sort()
- 如何处理linux终端中带有负号(-)的C++中的命令行参数
- 处理除以零会导致<csignal>意外行为
- 是否可以在c++中处理字符串流中的各个元素
- 在多个核心中处理一个HTTP请求