C++ 当我在递归函数中调用return时,我的程序意外地完成了

C++ 当我在递归函数中调用return时,我的程序意外地完成了,c++,recursion,C++,Recursion,我有一个递归函数,它遍历一个二叉树,当它到达一个属于叶的节点时返回该节点的值,但我的程序意外地在返回调用时结束。 这是我的密码: string decodeFile(int cha, ibitstream& input, HuffmanNode* encodingTree, HuffmanNode*& root, string& result) { if(root->isLeaf()) { char value = root->character;

我有一个递归函数,它遍历一个二叉树,当它到达一个属于叶的节点时返回该节点的值,但我的程序意外地在返回调用时结束。 这是我的密码:

string decodeFile(int cha, ibitstream& input, HuffmanNode* encodingTree, HuffmanNode*& root, string& result) {
  if(root->isLeaf()) {
    char value = root->character;
    string temp(1,value);
    cout << temp << endl;
    return temp;
  } else {
    if(cha == 0) root = root->zero;
    if(cha == 1) root = root->one;
    decodeFile(input.readBit(), input, encodingTree, root, result);
  }
}
string解码文件(int cha、ibitstream和input、huffmanode*编码树、huffmanode*根、字符串和结果){
如果(根->isLeaf()){
字符值=根->字符;
字符串温度(1,值);
不能一个;
解码文件(input.readBit(),input,encodingTree,root,result);
}
}

因此,我通过控制台检查发生了什么,此时它返回一个值,但当我转到main函数时,它什么也不返回。

好吧,您没有从递归调用返回值给调用方:

string decodeFile(int cha, ibitstream& input, HuffmanNode* encodingTree, HuffmanNode*& root, string& result) {
if(root->isLeaf()) {
    char value = root->character;
    string temp(1,value);
    cout << temp << endl;
    return temp;
} else {
    if(cha == 0) root = root->zero;
    if(cha == 1) root = root->one;
    // do calculation and return result!
    return decodeFile(input.readBit(), input, encodingTree, root, result);
}
现在,第二个
decodeFile
使用
return temp
返回值,但是第一个
decodeFile
没有向
主功能返回任何内容(因为它在调用
decodeFile
后退出):


main(NO RETURN)decodeFile好吧,您并没有从递归调用中返回值给调用者:

string decodeFile(int cha, ibitstream& input, HuffmanNode* encodingTree, HuffmanNode*& root, string& result) {
if(root->isLeaf()) {
    char value = root->character;
    string temp(1,value);
    cout << temp << endl;
    return temp;
} else {
    if(cha == 0) root = root->zero;
    if(cha == 1) root = root->one;
    // do calculation and return result!
    return decodeFile(input.readBit(), input, encodingTree, root, result);
}
现在,第二个
decodeFile
使用
return temp
返回值,但是第一个
decodeFile
没有向
主功能返回任何内容(因为它在调用
decodeFile
后退出):


main(NO RETURN)decodeFile好吧,您并没有从递归调用中返回值给调用者:

string decodeFile(int cha, ibitstream& input, HuffmanNode* encodingTree, HuffmanNode*& root, string& result) {
if(root->isLeaf()) {
    char value = root->character;
    string temp(1,value);
    cout << temp << endl;
    return temp;
} else {
    if(cha == 0) root = root->zero;
    if(cha == 1) root = root->one;
    // do calculation and return result!
    return decodeFile(input.readBit(), input, encodingTree, root, result);
}
现在,第二个
decodeFile
使用
return temp
返回值,但是第一个
decodeFile
没有向
主功能返回任何内容(因为它在调用
decodeFile
后退出):


main(NO RETURN)decodeFile好吧,您并没有从递归调用中返回值给调用者:

string decodeFile(int cha, ibitstream& input, HuffmanNode* encodingTree, HuffmanNode*& root, string& result) {
if(root->isLeaf()) {
    char value = root->character;
    string temp(1,value);
    cout << temp << endl;
    return temp;
} else {
    if(cha == 0) root = root->zero;
    if(cha == 1) root = root->one;
    // do calculation and return result!
    return decodeFile(input.readBit(), input, encodingTree, root, result);
}
现在,第二个
decodeFile
使用
return temp
返回值,但是第一个
decodeFile
没有向
主功能返回任何内容(因为它在调用
decodeFile
后退出):


main(NO RETURN)decodeFile您的函数无法
返回分支的
else
部分中的任何内容


如果您使用的是gcc,您可以让编译器使用
-Wreturn type
-Wall
选项警告您此类情况。

您的函数无法
返回分支的
else
部分中的任何内容


如果您使用的是gcc,您可以让编译器使用
-Wreturn type
-Wall
选项警告您此类情况。

您的函数无法
返回分支的
else
部分中的任何内容


如果您使用的是gcc,您可以让编译器使用
-Wreturn type
-Wall
选项警告您此类情况。

您的函数无法
返回分支的
else
部分中的任何内容



如果您使用的是gcc,您可以让编译器使用
-Wreturn type
-Wall
选项来警告您此类情况。

这一错误的常见程度令人惊讶。我不确定递归是什么让新手认为
return
是不必要的。这段代码不应该编译。你忘了打开“将警告视为错误”吗?我以前从未使用过。为什么它不应该编译?@ChristopherCreutzig根据标准,这段代码是需要编译的,除非编译器能够证明,不管输入是什么,它都会被
至少调用一次!root->isLeaf()
(并且
input.readBit()
返回)。@otch92 Chris说您应该始终注意编译器警告,因为它们通常指示实际错误(如本例中所示)或至少是错误的编码样式。最好的方法是打开“将警告视为错误”选项。这样,任何带有警告的代码都不会编译,因此您必须更正它们。令人惊讶的是,这种错误是多么常见。我不确定递归是什么让新手认为
return
是不必要的。这段代码不应该编译。你忘了打开“将警告视为错误”吗?我以前从未使用过。为什么它不应该编译?@ChristopherCreutzig根据标准,这段代码是需要编译的,除非编译器能够证明,不管输入是什么,它都会被
至少调用一次!root->isLeaf()
(并且
input.readBit()
返回)。@otch92 Chris说您应该始终注意编译器警告,因为它们通常指示实际错误(如本例中所示)或至少是错误的编码样式。最好的方法是打开“将警告视为错误”选项。这样,任何带有警告的代码都不会编译,因此您必须更正它们。令人惊讶的是,这种错误是多么常见。我不确定递归是什么让新手认为
return
是不必要的。这段代码不应该编译。你忘了打开“将警告视为错误”吗?我以前从未使用过。为什么它不应该编译?@ChristopherCreutzig根据标准,这段代码是需要编译的,除非编译器能够证明,不管输入是什么,它都会被
至少调用一次!root->isLeaf()
(并且
input.readBit()
返回)。@otch92 Chris说您应该始终注意编译器警告,因为它们通常指示实际错误(如本例中所示)或至少是错误的编码样式。最好的方法是打开“将警告视为错误”选项。这样,任何带有警告的代码都不会编译,因此您必须更正它们。令人惊讶的是,这种错误是多么常见。我不确定递归是什么让新手认为
return
是什么