Multiple inheritance problem

Hello, I have this code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class A
{
public:
    void a_function(){};
};


class B : public A
{

};


class C: public B, public A
{
   C();
};


C::C()
{
  a_function(); // error here
}


I get these errors:
1
2
3
 error: reference to 'a_function' is ambiguous
 error: candidates are: void A::a_function()
 error:                 void A::a_function()


Is there any way to solve this?

Thanks for help.
The mechanical way to solve this is to use virtual inheritance from A, but that's not a good solution. The question is what are you trying to accomplish?
Last edited on
have you heard of dimond shaped problem in c++ ?
we have to you virtual key word while inheriting the class

1
2
3
4
class C: virtual public B, virtual public A
{
   C();
};

+1 kbw

virtual inheritance may or may not be what OP needs, depends on the ultimate goal.
I need this for my SFML game. I have something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class GameObject : virtual public sf::Sprite
{
public:
  virtual void GameLoop(sf::RenderWindow &w)=0;
  // ...
};


class Player: virtual public sf::Sprite, virtual public GameObject
{
public:
   Player();
   void GameLoop(sf::RenderWindow &wnd){} // from GameObject
   // ...
};


Player::Player()
{
  Scale(0.1,0.1); // Inherited from sf::Sprite
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
std::vector<GameObject*> game_objects;

GameObject *player=new Player;
game_objects.push_back(player);
// ...

// call GameLoop function when needed
for(int i=0;i<game_objects.size();i++)
    game_objects[i]->GameLoop(wnd);


// Draw objects
for(int i=0;i<game_objects.size();i++)
    wnd.Draw(*game_objects[i]);



I'm not sure whether this is a good design.

Topic archived. No new replies allowed.