繁体   English   中英

C ++将布尔值返回为95

[英]C++ returning boolean as 95

在C ++中返回布尔值的问题。

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

显然,cout为FALSE意味着该函数返回false。。但是,当我打印出该函数的结果时,我得到95,它的计算结果为true。.为什么这样做呢?

谢谢

您错过了最后一个return语句,因此您得到了EAX低字节中的任何内容,这是随机垃圾。 您可能想要return true; 在您功能的最后。

您应该将编译器的警告级别告知您(这与“并非所有控制路径都返回值”类似)。

问题在于您的最终if语句:

if (node.letters[index] == NULL) {
    return false;
}
else {
    //if execution gets here, the return value of the function is undefined
    find (*node.letters[index],word.substr(1,(word.length() - 1)));
}

...也许尝试:

if (node.letters[index] == NULL) {
    return false;
}
else {
    return find (*node.letters[index],word.substr(1,(word.length() - 1)));
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM