无法取消引用指针以在 If 比较中使用值
我试图将 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的“input1”和“input2”是指向 Component 对象的指针。
除非您有 Component 的“==”运算符重载,否则它不知道如何将 == 与整数值一起使用。
在 C++ 中,0 被视为 false,任何除 0 之外的数字都被视为 true。使用此逻辑,您可以简单地编写函数如下,
如果有时无法将这些 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,
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.