C++递归不返回任何内容
C++ recursion returns nothing
string go(string s, int& ind, node* cur,char &c)
{
string f = "";
if (cur->leftChild == NULL && cur->rightChild == NULL)
{
f = cur-> content;
}
else
{
if (s[ind] == 0x30)
{
ind++;
go(s, ind, cur->leftChild,c);
}
else
{
ind++;
go(s, ind, cur->rightChild,c);
}
}
return f;// breakpoint here shows correct value 'e'
}
...
int main()
{
string h = "";
int ind = 0;
string h = go(s, ind, &glob_root,c);
cout << h << endl; // h is blank.
}
事实证明,f 命中的第一次断点,它显示值为"e",我想要什么,但随后它被命中时变为空白。
如果我将其更改为
string go(string s, int& ind, node* cur,char &c)
{
if (cur->leftChild == NULL && cur->rightChild == NULL)
{
return cur-> content;
}
else
{
if (s[ind] == 0x30)
{
ind++;
go(s, ind, cur->leftChild,c);
}
else
{
ind++;
go(s, ind, cur->rightChild,c);
}
}
}
我收到访问违规,因为我没有回报,如果我添加返回 ";最后,它只返回任何内容,而不是"e">
任何帮助表示赞赏
如果您在第一个代码块中点击else
分支,则没有任何内容会修改f
,因此它当然保持为空,并且该空字符串是您最终返回的内容。
您可能希望return go(...
或至少捕获返回值,并对其执行涉及f
或直接返回它的操作。
相关文章:
- 为什么 c++ 中的 main() 函数不采用除 int 和 void 之外的任何其他返回类型
- 是否有任何内置的哈希图函数来处理 c++ 中的值输入?
- 是否有任何内置函数可以检查给定的两个数字在给定整数数组中的顺序是否相同?
- 函数使用任何输入返回 0
- C++ 中是否有任何内置阶乘函数?
- C++中的函数是否有任何默认返回类型
- 是否有任何内置函数可以告诉编译器分支是否可预测
- 如何检查copy_if是否返回使用ostream_iterator范围内的任何内容
- vector::size() 如何在常量时间内返回向量的大小
- 我需要在C /C#中使用图形.我可以使用任何内置的C /C#库
- 是否有任何内置类可以处理C 中的大整数值
- c ++中是否有任何内置功能可以在计划的时间运行任务
- 是否有任何内置的 UTF 在 c++ 在窗口中
- 在VC++2010中执行任何内置的ARRAY函数
- C++:如何在不使用任何内置函数的情况下将字符串转换为整数
- 是否有任何内置的CUDA函数允许CUDA内核向主机代码报告错误?
- Io_getevents在小于timeout的时间内返回的作业数少于请求的作业数
- 我如何在常数时间(O(1))内获得字母表(1-26)中字符的数值/位置,而不使用任何内置方法或函数
- C++中的任何内置哈希方法
- c++库中的任何内置夹子方法