无法取消引用指针以在 If 比较中使用值

发布于 2025-01-13 18:38:34 字数 737 浏览 3 评论 0原文

我试图将 if 语句中的指针与 int 进行比较,以确定逻辑门的输出。我知道我需要遵循指针来比较实际值,但它不起作用,我不明白为什么。任何帮助将不胜感激。

bool TwoInputGate::getOutput(){
if(gateType == OR){
    if(input1 == 0 || input2 == 0)
        return false;
    else if(*input1 == 1 || *input2 == 1)
        return true;
}
else if(gateType == AND){

}
else if(gateType == XOR){

}
else
    return 0;

下面

是我收到的错误: C:\Users\cplutchak\Documents\LogicGateProject\TwoInputGate.cpp:24: error: invalid operands to binary expression ('Component' and 'int')

另外,这就是我的设置方式我的输入:

void TwoInputGate::setInput1(Component* in1){
input1 = in1;

}

void TwoInputGate::setInput2(Component* in2){
input2 = in2;

}

I am trying to compare a pointer to an int in my if statement for determining the output of logic gates. I know I need to deference the pointer to compare the actual value, but it's not working and I cannot figure out why. Any help would be appreciated.

bool TwoInputGate::getOutput(){
if(gateType == OR){
    if(input1 == 0 || input2 == 0)
        return false;
    else if(*input1 == 1 || *input2 == 1)
        return true;
}
else if(gateType == AND){

}
else if(gateType == XOR){

}
else
    return 0;

}

Below is the error I am getting: C:\Users\cplutchak\Documents\LogicGateProject\TwoInputGate.cpp:24: error: invalid operands to binary expression ('Component' and 'int')

Additionally this is how I am setting up my inputs:

void TwoInputGate::setInput1(Component* in1){
input1 = in1;

}

void TwoInputGate::setInput2(Component* in2){
input2 = in2;

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

身边 2025-01-20 18:38:34

您的“input1”和“input2”是指向 Component 对象的指针。
除非您有 Component 的“==”运算符重载,否则它不知道如何将 == 与整数值一起使用。

在 C++ 中,0 被视为 false,任何除 0 之外的数字都被视为 true。使用此逻辑,您可以简单地编写函数如下,

bool TwoInputGate::getOutput(){
    if(input1 == nullptr || input2 == nullptr){
        return false;
    }

    if(gateType == OR){
        return input1->getOutput() || input2->getOutput();
    }
    else if(gateType == AND){
        
    }
    else if(gateType == XOR){
        
    }
    else
        return false;
    }
}

如果有时无法将这些 input1 和 input2 设置为任何内容,则在访问值之前首先检查“nullptr”。

Your "input1" and "input2" are pointers pointing to the Component object.
Unless you have an "==" operator overloading for Component, it does not know how to use == with an integer value.

In c++, 0 is treated as false, and any other number than 0 as true. Using this logic you can simply write your function as following,

bool TwoInputGate::getOutput(){
    if(input1 == nullptr || input2 == nullptr){
        return false;
    }

    if(gateType == OR){
        return input1->getOutput() || input2->getOutput();
    }
    else if(gateType == AND){
        
    }
    else if(gateType == XOR){
        
    }
    else
        return false;
    }
}

If there are times when those input1 and input2 could not have been set to anything, then check for "nullptr" first before accessing the values.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文