Home > OS >  Override of virtual function ambiguous with diamond inheritance
Override of virtual function ambiguous with diamond inheritance

Time:12-14

I have a diamond inheritance structure like:

class Card {
public:
    virtual std::string info() const = 0;
}

class Color : public Card {
public:
    virtual std::string info() const = 0;
}

class Deploy : virtual public Color {
public:
    virtual std::string info() const = 0;
}

class Attack : virtual public Color {
public:
    virtual std::string info() const = 0;
}

class Character : public Attack, public Deploy {
public:
    std::string info() { return "My Info" };
}

But in the class Character it says to me:

override of virtual function "Card::info" is ambiguous

I want to have just one declaration of info in Character. How can I do it?

CodePudding user response:

Apart from missing some ;s at the end of class definitions and the return statement of Character::info, The main issue is that are missing the const qualifier from the definition of Character::info.

I would also add override the get help from the compiler if the method does not in fact override a base class method:

//-----------------vvvvv-vvvvvvvv----------------------
std::string info() const override { return "My Info"; };
  •  Tags:  
  • c
  • Related