samedi 22 juillet 2017

C++ invalid operands to binary expression ('IOperand *' and 'IOperand *')

I know the question has been asked many times but I can't find an answer for my problem,

I have this operator overload:

virtual IOperand *operator+(const IOperand &rhs) const = 0; //sum

from this interface:

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() {}
};

this interface is inherited and overwritten by 6 classes "Int8", "Int16", "Int32", "Float", "Double", "BigDecimal" like that:

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

Here is how is written this operator+ in the Int8 class

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

for me, till here it seems pretty good but when I try to use the operator+ like that:

IOperand *first = stack_.top();
IOperand *second = stack_.top();

IOperand *result = first + second;

I have the error:

invalid operands to binary expression ('IOperand *' and 'IOperand *')

in

IOperand *result = first + second;

I'm having a really hard time figuring out what is happening, help pls

ps: stack_ is an std::stack and yes it's not empty

Aucun commentaire:

Enregistrer un commentaire