在 C++ 中返回 boolean 值的问题..
bool find( const TrieNode &node, const string word )
{
if (word.length() == 0)
{
if (node.isWord)
{
cout << "TRUE" << endl;
return true;
}
else
{
cout << "FALSE" << endl;
return false;
}
}
char firstletter = word.at(0);
int index = firstletter - 'a';
if (node.letters[index] == NULL)
{
return false;
}
else
{
find (*node.letters[index],word.substr(1,(word.length() - 1)));
}
}
我主要有
cout << find(*mynode,"word") << endl;
将屈服于:
错误
95
显然,FALSE 的 cout 意味着该函数返回 false。但是,当我打印出该函数的结果时,我得到 95,其计算结果为 true。它为什么会这样做?
谢谢
最佳答案
您缺少最后的返回语句,所以您得到的是 EAX
低字节中的任何内容,这是随机垃圾。您可能希望在函数的最后 return true;
。
您应该提高编译器的警告级别,因为它应该告诉您这一点(类似于“并非所有控制路径都返回一个值”)。
关于C++ 返回 boolean 值 95,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6381799/