Youssef Bouhjira
Youssef Bouhjira

Reputation: 1631

Unable to pass a subclass instance as argument instead of superclass

In my code I have class McdGraphicsScene that inherits from QGraphicsScene, but when I try to pass a pointer to an instance of McdGraphicsScene to QGraphicsView::setScene(QGraphicsScene* scene); I get the following error:

../MeriseModeler/merisemodeler/mcdui.cpp: In member function 'void McdUi::setModel(McdModel*)':
../MeriseModeler/merisemodeler/mcdui.cpp:34:42: error: no matching function for call to 'QGraphicsView::setScene(McdGraphicsScene*)'
../MeriseModeler/merisemodeler/mcdui.cpp:34:42: note: candidate is:
In file included from ../../.qt5/5.0.0/gcc/include/QtWidgets/QGraphicsView:1:0,
                 from ../MeriseModeler/merisemodeler/mcdui.cpp:10:
../../.qt5/5.0.0/gcc/include/QtWidgets/qgraphicsview.h:161:10: note: void QGraphicsView::setScene(QGraphicsScene*)
../../.qt5/5.0.0/gcc/include/QtWidgets/qgraphicsview.h:161:10: note:   no known conversion for argument 1 from 'McdGraphicsScene*' to 'QGraphicsScene*'

here is the code of the class

class McdGraphicsScene : public QGraphicsScene
{
    Q_OBJECT

    // Methods and attributs
};

Upvotes: 3

Views: 576

Answers (1)

Alex B
Alex B

Reputation: 84962

Are you using any forward declarations of McdGraphicsScene?

Make sure that the actual class definition is visible at the call site. If there is only forward declaration available, the calling code cannot deduce that McdGraphicsScene inherits from QGraphicsScene.

Upvotes: 8

Related Questions