0

So, my class hierarchy looks something like this:

     QObject  QGraphicsPolygonItem
           \  /
            \/
          CBubble
            /\
           /  \
 CListBubble  CSingleLinkBubble
           \  /
            \/
       CActionBubble

The reason I'm doing multiple inheritance is because I have another bubble that is a CListBubble (LB), but not a CSingleLinkBubble (SB).

The error I receive is "CBubble is an ambiguous base of CActionBubble".

  • I've tried the virtual tag on one or both of them.
  • I've tried adding "virtual CBubble" to CActionBubble.
  • I've tried changing to "public virtual CBubble" in LB and SB, but that causes issues in all bubbles that inherit from them.

I could forego CListBubble entirely and copy/pasta the code, but that goes against all my instincts. I could try to refactor this to use the decorator pattern, but that seems like a lot of unnecessary work for this one case.

Any help or suggestions are much appreciated. Thanks.

EDIT: I apologize, should have read the MCVE rules.
Note - I just typed this up now, so hopefully there aren't any syntax issues, but I assume it gets the point across.

clistbubble.h

// header guard
#include "cbubble.h"

class CListBubble : public CBubble
{
public:
    CListBubble(const QPointF & pos, QGraphicsItem *parent = 0);

    // data members/methods
};

clistbubble.cpp

#include "clistbubble.h"

CListBubble(const QPointF & pos, QGraphicsItem *parent)
  : CBubble(pos, parent) // rest of base member initialization
{
       // other stuff for this type of bubble
}

// Other methods

The header for CSingleLinkBubble is equivalent in all but members and methods.

cactionbubble.h

// header guard
#include "csinglelinkbubble.h"
#include "clistbubble.h"

class CActionBubble : public virtual CSingleLinkBubble, public virtual CListBubble
{
public:
    CActionBubble(const QPointF & pos, QGraphicsItem *parent);

    // data members/methods
};

cactionbubble.cpp

#include "cactionbubble.h"

CActionBubble(const QPointF & pos, QGraphicsItem *parent)
  : CSingleLinkBubble(pos, parent), CListBubble(pos, parent) // rest of base member initialization
{
       // other stuff for this type of bubble
}

I'm concerned that the issue lies in the constructor of CActionBubble where both parent classes are called. However both calls are necessary and would happen anyway if I implemented a default ctor.

The error points me to the place I attempt to new a CActionBubble. "CBubble is an ambiguous base of CActionBubble"

I also forgot to mention that CBubble inherits from QObject so I can implement signals and slots. I read something awhile back that multiple inheritance with QObject is not possible. Could it be that this is the actual issue, and the error just points one layer below?

Garrett Fleischer
  • 177
  • 1
  • 3
  • 13
  • *Where* do you get the error? Can you please show us the code (preferably a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve))? Is that the *complete* and *only* error message from the compiler? Please include the *complete* and *unedited* build log. – Some programmer dude May 11 '16 at 06:21
  • The usual solution is the third one you mentioned, why it causes issues in the other classes that inherit from them is another matter to be cleared. – Matteo Italia May 11 '16 at 06:22
  • 1
    Your picture corresponds to the third option **only**. The first two options correspond to having **two** `CBubble` instances in each `CActionBubble`. `but that causes issues in all bubbles that inherit from them.` Yes, that's how stuff works in C++. You need to deal with it. – n. m. could be an AI May 11 '16 at 06:29
  • "refactor this to use the decorator pattern" This could work but I suspect (not sure) this will come to the same "issues in all bubbles that inherit from them" in a different wrap. The issues are not here because of Stroustrup's whims, they are inherent in the MI design, no matter how you implement it. – n. m. could be an AI May 11 '16 at 07:09
  • @n.m. Ok, could you please elaborate a little on how I should deal with it? – Garrett Fleischer May 11 '16 at 18:32
  • @JoachimPileborg yes, that is the only error aside from some unused variable warnings in methods I haven't finished stubbing out yet. – Garrett Fleischer May 11 '16 at 18:40
  • Alright, so it seems like I need to inherit virtually all the way through. And make sure that subclasses scope their methods appropriately. Is this correct? – Garrett Fleischer May 11 '16 at 18:42
  • Show the issues you have... – n. m. could be an AI May 12 '16 at 04:01
  • You cannot inherit from QObject through multiple base classes. [See this question for why](http://stackoverflow.com/questions/8578657/qobject-multiple-inheritance). – jonspaceharper May 13 '16 at 03:57
  • Well, thanks for the help guys. I decided to take the advice given to me by my professor, "if you are using multiple inheritance, you probably need to rethink your approach." I did, and came to the conclusion that my two "list bubbles" need to be handled differently anyway, (one requires a custom QAbstractListModel). So, thanks again, but I'm using a different approach. :) – Garrett Fleischer May 13 '16 at 05:29

0 Answers0