[英]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.