如何在非bool函数中通过引用传递bool变量?

Nic*_*dey 3 c++

我看了如何通过引用传递bool,但该函数返回了它内部的bool.我也在这里查看堆栈溢出,但评论者提出的建议并没有改善我的情况.所以,

我有一个功能:

myTreeNode* search_tree(myTreeNode *test_irater, char p_test, bool &flag)
Run Code Online (Sandbox Code Playgroud)

这显然会返回一种myTreeNode*类型.我也有一个变量,bool flag我想改变函数内的值.但是,当我尝试通过引用传递bool时,我收到错误消息

错误:从'bool*'|类型的右值开始,无效初始化类型为'bool&'的非const引用

如何通过引用传递bool而不返回bool?我正在使用最新版本的CodeBlocks,如果这是相关的.

编辑:代码

myTreeNode* search_tree(myTreeNode *test_irater, char p_test, bool &flag)
{
    switch(p_test) 
    {
    case 'a':
        if (test_irater->childA == NULL)
            flag = false;
        else {
            test_irater = test_irater->childA;
            flag = true;
        }
        break;
    case 't':
        if (test_irater->childT == NULL)
            flag = false;
        else {
            test_irater = test_irater->childT;
            flag = true;
        }
        break;
    case 'c':
        if (test_irater->childC == NULL)
            flag = false;
        else {
            test_irater = test_irater->childC;
            flag = true;
        }
        break;
    case 'g':
        if (test_irater->childG == NULL)
            flag = false;
        else {
            test_irater = test_irater->childG;
            flag = true;
        }
        break;
    }
    return test_irater;
}
Run Code Online (Sandbox Code Playgroud)

叫做:

test_irater = search_tree(test_irater, p_test, &flag); 
Run Code Online (Sandbox Code Playgroud)

Jts*_*Jts 6

您正在使用addressof(&)运算符,意思&flag是转换为bool*

删除它,它应该工作:

test_irater = search_tree(test_irater, p_test, flag); 
Run Code Online (Sandbox Code Playgroud)