错误 C2679:二进制"+":找不到采用类型右侧操作数的运算符

Error C2679: binary '+' : no operator found which takes a right-hand operand of type

本文关键字:操作数 运算符 类型 二进制 C2679 找不到 错误      更新时间:2023-10-16

首先,是的,这是我正在努力完成的家庭作业,所以不胜感激。我们正在C++制作一个计算器,该计算器在 + 和 - 运算符上应该以不同的方式运行。使用"+",它应该将两个数字相加(即 45 + 54 = 4554)。使用"-",它应该从第二个元素中删除第一个元素的第一个数字(即 1217 - 1 = 27)我们应该通过重载 + 和 - 运算符来做到这一点,我似乎正在努力解决这个问题。提前感谢您的帮助!

class WhackyRPN
{
public:
    int value;
    int operator+ (WhackyRPN a[]);
    int operator- (WhackyRPN s[]);
    int getValue();
    void setValue(int);
};
void WhackyRPN::setValue(int val){
    value = val;
}
int WhackyRPN::getValue(){
    return value;
}
int WhackyRPN::operator+ (WhackyRPN a[]){
    string combinedNum = to_string(a[1].getValue()) + to_string(a[0].getValue());
    int finalNum = stoi(combinedNum);
    return finalNum;
}
int WhackyRPN::operator- (WhackyRPN s[]){
    int minusNum;
    string firstNum = to_string(s[0].getValue());
    string secondNum = to_string(s[1].getValue());
    string minusString = to_string(minusNum);
    for (int i = 0; i < firstNum.length(); i++){
        if (firstNum.at(0) != secondNum.at(i)){
            minusString.at(i) += secondNum.at(i);
        }
    }
    minusNum = stoi(minusString);
    return minusNum;
}
int main()
{
    WhackyRPN stackPos[4];
    string indent = "     ";
    string userInput;
    stackPos[0].setValue(0);
    stackPos[1].setValue(0);
    stackPos[2].setValue(0);
    stackPos[3].setValue(0);
    while (1){
        system("cls");
        cout << "---STACK---" << endl;
        cout << indent << stackPos[3].getValue() << endl;
        cout << indent << stackPos[2].getValue() << endl;
        cout << indent << stackPos[1].getValue() << endl;
        cout << indent << stackPos[0].getValue() << endl;
        cout << "CMD: ";
        cin >> userInput;
        if (userInput == "exit" || userInput == "Exit" || userInput == "EXIT"){
            exit(0);
        }
        switch (userInput[0]){
        case 'q':
        case 'Q':
            exit(0);
        case 'p':
        case 'P':
            stackPos[0] = stackPos[1];
            stackPos[1] = stackPos[2];
            stackPos[2] = stackPos[3];
            break;
        case '1':
        case '2':
        case '3':
        case '4':
        case '5':
        case '6':
        case '7':
        case '8':
        case '9':
        case '0':
            stackPos[3].setValue(stackPos[2].getValue());
            stackPos[2].setValue(stackPos[1].getValue());
            stackPos[1].setValue(stackPos[0].getValue());
            stackPos[0].setValue(stoi(userInput));
            break;
        case '+': //combine pos[1] and pos[0];
            int finalNum = stackPos[1] + stackPos[0];
            stackPos[3].setValue(stackPos[2].getValue());
            stackPos[2].setValue(stackPos[1].getValue());
            stackPos[1].setValue(stackPos[0].getValue());
            stackPos[0].setValue(finalNum);
            break;
        case '-': //remove pos[0].firstNum from pos[1]
            int minusNum = stackPos[0] - stackPos[1];
            stackPos[3].setValue(stackPos[2].getValue());
            stackPos[2].setValue(stackPos[1].getValue());
            stackPos[1].setValue(stackPos[0].getValue());
            stackPos[0].setValue(minusNum);
            break;
        case '/': //divide pos[1] by pos[0]
            if (stackPos[0].getValue() == 0){
                cout << "Cannot divide by 0" << endl;
                system("pause");
                break;
            }
            int endQuotient = stackPos[1].getValue() / stackPos[0].getValue();
            stackPos[3].setValue(stackPos[2].getValue());
            stackPos[2].setValue(stackPos[1].getValue());
            stackPos[1].setValue(stackPos[0].getValue());
            stackPos[0].setValue(endQuotient);
            break;
        case '*':   //multiply pos[1] by pos[0]
            int endProduct = stackPos[1].getValue() * stackPos[0].getValue();
            stackPos[3].setValue(stackPos[2].getValue());
            stackPos[2].setValue(stackPos[1].getValue());
            stackPos[1].setValue(stackPos[0].getValue());
            stackPos[0].setValue(endProduct);
            break;
        default:
            break;
        }
    }
    system("pause");
    return 0;
}

你得到的错误,因为你确实没有stackPos[1] + stackPos[0]可以解决的operator+过载。您拥有的唯一重载是 WhackyRPN::operator+(WhackyRPN*); 类型(即使您编写了数组,它也是一个指针 - 阅读此处和此处。但这与这个问题无关。签名应WhackyRPN::operator+(WhackyRPN)。更惯用的是WhackyRPN::operator+(const WhackyRPN&).欲了解更多信息,请阅读这个伟大的答案。

替换

int finalNum = stackPos[1] + stackPos[0];

int finalNum = stackPos[1].getValue() + stackPos[0].getValue();

在你的程序中,你有对象数组 stackPos[] ,它有函数 setValue()getValue()分别获取和返回整数。您需要在此处使用getValue()因为数组元素本身不是整数,而是对象。

这也正是您的错误语句所说的。但是您似乎已经知道这一点,因为您已经在*/操作中实现了它。

希望这有帮助。

相关文章: