无法将子类实例作为参数而不是超类传递

时间:2013-03-06 12:25:02

标签: c++ qt inheritance polymorphism

在我的代码中,我的类McdGraphicsScene继承自QGraphicsScene,但当我尝试将指针传递给McdGraphicsScene的实例到QGraphicsView::setScene(QGraphicsScene* scene);时,我得到以下内容错误:

../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*'

这是类

的代码
class McdGraphicsScene : public QGraphicsScene
{
    Q_OBJECT

    // Methods and attributs
};

1 个答案:

答案 0 :(得分:8)

您使用的是McdGraphicsScene的任何forward declarations吗?

确保在呼叫站点上可以看到实际的类定义。如果只有前向声明可用,则调用代码无法推断出McdGraphicsScene继承自QGraphicsScene

相关问题