0

CORRECTION :

I edited two line from:

1) "class circle : public shape" to "class circle : public virtual shape"

2)"class square : public shape" to "class square : public virtual shape"

And yes , i am trying to have only one instance of Shape class for the shapes class , while defining the method draw differently inside Circle class and Square class


I am trying to do a simple inheritance program , but it gave me the following error:

*error C2250: 'shapes' : ambiguous inheritance of 'void shape::draw(void)'
*IntelliSense: override of virtual function "shape::draw" is ambiguous

-->this code resembles the solution of the diamond problem. I don't get why i see this error.

Here is the code:

    #include<iostream>
    using namespace std;
    class shape
    {
    public:
        shape()
        {
            cout << "shape created" << endl;
        }
        virtual void  draw()=0;


    };

    class circle : public virtual shape
    {
    public:
        circle()
        {
            cout << "circle created" << endl;
        }
        virtual void  draw()
        {
            cout << "this is a circle" << endl;
        }
    };

    class square : public virtual  shape
    {
    public:
        square()
        {
            cout << "square created" << endl;
        }
        virtual void  draw()
        {
            cout << "this is a square" << endl;
        }
    };

    class shapes : public  circle, public  square
    {
    public:
        shapes()
        {
            cout << "shapes created" << endl;
        }
    };

    void main()
    {
        shapes e;
        cout << "-------------" << endl;
        system("pause");
    }
Yehia Hesham
  • 13
  • 1
  • 5
  • Which draw you want in `shapes` ? – Jagannath Apr 04 '15 at 16:24
  • I can't add an answer, but perhaps you are looking for virtual inheritance, after which you'd then provide your own `draw` method in the `shapes` class? Something like this: [http://ideone.com/lkIXIF](http://ideone.com/lkIXIF) – AndyG Apr 04 '15 at 16:32
  • although now I realized it's not a dupe... vote to reopen, the problem is because you have ambiguity in `draw()`. You are inheriting from 2 classes, both of which overload `draw`. You have to implement your own `draw` in the most derived class, see @AndyG comment/code – vsoftco Apr 04 '15 at 16:32
  • @πάνταῥεῖ exactly, me too :) – vsoftco Apr 04 '15 at 16:38
  • @Yehia, use `int main` instead of `void main` (we are in 2015 not 1989), and `std::cin.get();` instead of the platform-dependent `system("pause");` – vsoftco Apr 04 '15 at 16:52

1 Answers1

1

(Moving from comment to answer)

It appears you intended to inherit virtually from shape, and then provide your own draw function in shapes

Inherit virtually like so:

class circle : public virtual shape //and same for class square

Then in shapes:

class shapes : public  circle, public  square
{
public:
    shapes()
    {
        cout << "shapes created" << endl;
    }

    virtual void draw() //'virtual' is optional here
    {
        circle::draw();
        square::draw();
    }
};

Live Demo

Edit

Using virtual inheritance in your case is not strictly necessary per se, because your base is abstract (only pure virtual methods). However, if your use case is that the base class implements methods, then you will definitely want to use virtual inheritance. (Thanks @vsoftco)

This is because virtual inheritance guarantees that only one base class instance will be inherited into shapes, whereas in C++ by default in each derived class gets its own instance of a base, and so shapes would actually inherit TWO instances of shape, one through circle and one through square. At that point, calling any base class function from a shapes object becomes ambiguous because the compiler wouldn't be certain which instance you meant to call it from.

AndyG
  • 39,700
  • 8
  • 109
  • 143
  • Indeed, that's the problem. In this case it works even without virtual inheritance (although it's a good idea to use it in multiple inheritance) – vsoftco Apr 04 '15 at 16:47
  • @vsoftco: Good point about virtual inheritance. I wanted to be safe. I'll edit the post. – AndyG Apr 04 '15 at 16:48
  • ok it is working , but why did it generate a problem in the beginning ? – Yehia Hesham Apr 04 '15 at 18:36
  • Do I need to Define it even if i don't want this class to implement draw function and thanks. – Yehia Hesham Apr 04 '15 at 18:37
  • @YehiaHesham: Yes, because the function would otherwise still exist through the inheritance chain, which would make a call to `e.draw()` ambiguous. – AndyG Apr 04 '15 at 20:11