简体   繁体   中英

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

First up, yes, this is homework that I'm struggling with, so help would be appreciated. We're making a calculator in C++ that is supposed to function differently on the + and - operators. With '+' it is supposed to to add the two numbers together (ie, 45 + 54 = 4554). With '-' it is supposed to remove the first digit of the first element from the second element (ie, 1217 - 1 = 27) We're supposed to do this by overloading the + and - operators, which I seem to be struggling with. Thanks in advance for the help!

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;
}

You get the error that you do because there really is no overload of operator+ which stackPos[1] + stackPos[0] could resolve to. The only overload you have is of type WhackyRPN::operator+(WhackyRPN*); (it is a pointer even though you have written an array - read here and here . But that isn't relevant to this question.) The signature should be WhackyRPN::operator+(WhackyRPN) . More idiomatic would be WhackyRPN::operator+(const WhackyRPN&) . For more, read this great answer .

replace

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

with

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

In your program, you have the array of objects stackPos[] , which has functions setValue() and getValue() which take and return integer respectively. You need to use getValue() here as the array elements themselves are not integer, they are objects.

This is exactly what your error statement is saying as well. But you seem to already know that because you have implemented it in * and / operations.

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM