【问题标题】:Error C2679: binary '+' : no operator found which takes a right-hand operand of type错误 C2679:二进制“+”:未找到采用右侧操作数类型的运算符
【发布时间】:2015-01-10 16:45:27
【问题描述】:

首先,是的,这是我正在努力完成的作业,因此我们将不胜感激。我们正在用 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;
}

【问题讨论】:

  • 你的问题到底是什么?
  • 感谢您诚实并提出相关问题寻求帮助,即使是家庭作业。附带说明:在现实世界中,重载 +/- 等运算符来执行非直观操作(与加法/减法无关)将被视为不好的做法。

标签: c++ class operator-overloading


【解决方案1】:

您会得到您所做的错误,因为确实没有 operator+ 的过载 stackPos[1] + stackPos[0] 可以解决。您拥有的唯一重载类型为WhackyRPN::operator+(WhackyRPN*);(即使您编写了一个数组,它也是一个指针 - 读取herehere。但这与这个问题无关。)签名应该是@987654328 @。更惯用的是WhackyRPN::operator+(const WhackyRPN&amp;)。更多信息,请阅读这篇精彩的answer

【讨论】:

  • 这个答案让我走上了正确的道路,弄清楚我需要如何完成它。这个特定的link 也有很大帮助。非常感谢您为我指明了正确的方向!
【解决方案2】:

替换

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

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

在你的程序中,你有一个对象数组stackPos[],它有函数setValue()getValue(),它们分别接受和返回整数。您需要在这里使用getValue(),因为数组元素本身不是整数,它们是对象。

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

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多