Je sais que la question a été posée à plusieurs reprises, mais je ne peux pas trouver une réponse à mon problème,C++ opérandes non valides à l'expression binaire (« IOperand * » et « IOperand * »)
J'ai cette surcharge de l'opérateur :
virtual IOperand *operator+(const IOperand &rhs) const = 0; //sum
de cette 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() {}
};
cette interface est héritée et remplacée par 6 classes "Int8", "Int16", "Int32", "Float", "Double", « BigDecimal "comme ça:
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;
};
Voici comment est écrit cet opérateur + dans le pour moi, classe Int8
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));
}
jusqu'à ici, il semble assez bon, mais quand j'essaie d'utiliser le + comme opérateur que:
IOperand *first = stack_.top();
IOperand *second = stack_.top();
IOperand *result = first + second;
J'ai l'erreur:
invalid operands to binary expression ('IOperand *' and 'IOperand *')
dans
IOperand *result = first + second;
Je vais avoir vraiment du mal à comprendre ce qui se passe, aider pls
ps: stack_ est un std :: pile et oui ce n'est pas vide
Vous ne pouvez pas surcharger les opérateurs pour des pointeurs, de quelque type que ce soit. –
Et ajouter deux pointeurs n'a pas de sens. –
même en le déréférenciant comme ça: IOperand * result = first + (* second); J'obtiens des opérandes invalides à l'expression binaire ('IOperand *' et 'IOperand') –