Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Override of virtual function ambiguous with diamond inheritance

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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"; };
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading