[英]C++ invalid operands to binary expression ('IOperand *' and 'IOperand *')
我知道这个问题已经问了很多遍了,但是我找不到我的问题的答案,
我有这个运算符重载:
virtual IOperand *operator+(const IOperand &rhs) const = 0; //sum
从此界面:
class IOperand
{
public:
virtual std::string toString() const = 0; //stringthatrepresentstheinstance
virtual eOperandType getType() const = 0; //returnsthetypeofinstance
virtual IOperand *operator+(const IOperand &rhs) const = 0; //sum
virtual IOperand *operator-(const IOperand &rhs) const = 0; //difference
virtual IOperand *operator*(const IOperand &rhs) const = 0; //product
virtual IOperand *operator/(const IOperand &rhs) const = 0; //quotient
virtual IOperand *operator%(const IOperand &rhs) const = 0; //modulo
virtual ~IOperand() {}
};
该接口被6个类“ Int8”,“ Int16”,“ Int32”,“ Float”,“ Double”,“ BigDecimal”继承和覆盖,如下所示:
class Int8 : public IOperand
{
private:
std::string valueUnmodified;
int8_t value;
public:
Int8(std::string my_value);
virtual ~Int8();
virtual std::string toString() const;
virtual eOperandType getType() const;
virtual IOperand *operator+(const IOperand &rhs) const;
virtual IOperand *operator-(const IOperand &rhs) const;
virtual IOperand *operator*(const IOperand &rhs) const;
virtual IOperand *operator/(const IOperand &rhs) const;
virtual IOperand *operator%(const IOperand &rhs) const;
};
这是在Int8类中写这个operator +的方法
IOperand *Int8::operator+(const IOperand &rhs) const
{
if (rhs.getType() != eOperandType::INT8)
throw avm::AvmError("Operator error");
int8_t nb;
nb = std::stoi(rhs.toString());
int8_t result;
result = this->value + nb;
return new Int8(std::to_string(result));
}
对我来说,到这里为止看起来还不错,但是当我尝试像这样使用operator +时:
IOperand *first = stack_.top();
IOperand *second = stack_.top();
IOperand *result = first + second;
我有错误:
invalid operands to binary expression ('IOperand *' and 'IOperand *')
在
IOperand *result = first + second;
我很难弄清楚发生了什么,请帮助
ps:stack_是std :: stack,是的,它不为空
操作员+需要根据您的原型提供参考
virtual IOperand *operator+(const IOperand &rhs) const;
但是您尝试添加指针:
IOperand *first = stack_.top();
IOperand *second = stack_.top();
IOperand *result = first + second;
// should be
IOperand *result = *first + *second;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.