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

标签 c++ class operator-overloading

首先,是的,这是我正在努力完成的作业,因此我们将不胜感激。我们正在用 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*);(它是一个指针,即使你已经写了一个数组 - 阅读 herehere 。但那不是'与此问题无关。)签名应为 WhackyRPN::operator+(WhackyRPN)。更惯用的是 WhackyRPN::operator+(const WhackyRPN&)。有关更多信息,请阅读这篇精彩的 answer .

关于c++ - 错误 C2679 : binary '+' : no operator found which takes a right-hand operand of type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27878571/

相关文章:

javascript - angularjs工厂内部函数不依赖于工厂吗?

java - 从 Main 调用私有(private)类(class)。均在公开课

类内的 Python 回调

c++ - 如何重载 operator[] 以允许 `object[a] = b` ?

c++ - 如何重载运算符=

c++ - 使用 switch 语句加密

c++ - 在 C++ 中绑定(bind) C 函数( undefined reference )

C++ 双下标运算符重载

c++ - 转发声明文件 *

c++链接没有运行时库的STL