无法从指针派生类转换为指针基类(多态性)
Cannot convert from pointer derived class to pointer base class (polymorphism)
我有上面描述的问题,但原因显然在其他地方,因为这在以前是有效的,除了最小化项目的include和forward声明之外,我没有做任何重大更改。
我有几个类,都是从一些QObjects
和UIElementInterface
派生而来的。在以下示例中,QCheckBox
#ifndef CHECKBOX_H
#define CHECKBOX_H
#include <QCheckBox>
#include "uielementinterface.h"
#include <QString>
class QPoint;
class QSize;
class QWidget;
class CheckBox : public QCheckBox, public UIElementInterface
{
Q_OBJECT
//...
public:
CheckBox(const QString& label, const QString& define, const QString& header, const QPoint& pos, const QSize& size, QWidget *parent = 0);
};
#endif
和UIElementInterface
#ifndef UIELEMENTINTERFACE_H
#define UIELEMENTINTERFACE_H
class UIElementInterface
{
public:
virtual ~UIElementInterface(){}
UIElementInterface(const QString& define, const QString& header);
//...
};
#endif
在XmlReader
中,我转发声明了CheckBox
(和UIElementInterface
)。这里我通过指针传入CheckBox*
作为其基础UIElementInterface
void XmlReader::readCheckBox(ContainerInterface* container, const QString& header)
{
Q_ASSERT(xml.isStartElement() && xml.name() == "checkbox");
QXmlStreamAttributes attr = xml.attributes();
CheckBox* checkBox = container->createCheckBox(getLabel(attr), getDefine(attr), getHeader(attr, header), getTopLeft(attr), getSize(attr));
m_centralWidget->setUIElement(getDefine(attr), checkBox); //<--Compiler Error, used to work fine.
xml.readElementText();
}
我在CheckBox
和UIElementInterface
构造函数中进行了一些打印,以验证两者的初始化是否正确。
以下是setUIElement
的实现:
void CentralWidget::setUIElement(const QString& define, UIElementInterface* UIElement)
{
m_uiElementMap[define] = UIElement;
}
从XmlReader
调用它会给我:
error: no matching function for call to 'CentralWidget::setUIElement(QString, CheckBox*&)'
从CheckBox
到UIElementInterface
的static_cast
将给出invalid static_cast
错误,而隐式强制转换将给我一个cannot convert ... in initialization
错误。
正如我所说,同样的继承模式过去也起作用,据我所知,它是应该起作用的。因此:我缺少什么
提前谢谢。
编辑
以下是编译器输出(用于调用setUIElement
):
../src/xmlreader.cpp:278: error: no matching function for call to 'CentralWidget::setUIElement(QString, CheckBox*&)'
../include/centralwidget.h:40: note: candidates are: void CentralWidget::setUIElement(const QString&, UIElementInterface*)
以及CentralWidget
的类定义
/*!
* class CentralWiget
* brief The CentralWidget class is a subclass ofQWidget.
*/
#ifndef CENTRALWIDGET_H
#define CENTRALWIDGET_H
#include <QWidget>
#include "containerinterface.h"
#include <QMap>
#include <QString>
class QHBoxLayout;
class UIElementInterface;
class QPoint;
class QSize;
class PushButton;
class CheckBox;
class ComboBox;
class Image;
class Led;
class Text;
class TabWidget;
class QTabWidget;
class TreeWidget;
class CentralWidget : public QWidget, public ContainerInterface
{
Q_OBJECT
private:
QHBoxLayout* m_layout;
QMap<QString, UIElementInterface*> m_uiElementMap;
public:
CentralWidget(QWidget* parent = 0);
~CentralWidget();
void setUIElement(const QString& define, UIElementInterface* UIElement);
UIElementInterface* getUIElement(const QString& define);
void createHorizontalLayout();
void addWidgetToLayout(QWidget* widget);
PushButton* createButton(const QString &label, const QString &define, const QPoint& topLeft, const QSize& size);
CheckBox* createCheckBox(const QString &label, const QString &define, const QString &header, const QPoint& topLeft, const QSize& size);
ComboBox* createComboBox(const QString &label, const QString &define, const QString &header, const QPoint& topLeft, const QSize& size);
Image* createImage(const QString &file, const QString &define, const QPoint& topLeft, const QSize& size);
Led* createLed(const QString &define, const QString &onColor, const QString &offColor, const QPoint& topLeft, const QSize& size);
Text* createText(const QString &define, const QString &label, const QPoint& topLeft, const QSize& size);
TabWidget* createTabWidget(const QPoint& topLeft, const QSize& size);
TreeWidget* createTreeWidget(const QStringList& labels, const QPoint& topLeft, const QSize& size);
void connectUIElement(const QString& signalName, const QString& slotName, UIElementInterface* UIElement);
private slots:
void setDef(QString s);
void sendCom(QString s);
};
#endif
`
转动m_centralWidget->setUIElement(getDefine(attr), checkBox);
进入
m_centralWidget->setUIElement(getDefine(attr), (UIElementInterface*)checkBox);
相关文章:
- 如何使用基类指针引用派生类成员
- 使用基类指针创建对象时,缺少派生类析构函数
- 如果基类包含双指针成员,则派生类的构造函数
- 使用基类指针调用基类的值构造函数的语法是什么?
- 如何在基类指针向量的元素上应用重载的多态函数
- 如何使用 std::make_shared 创建基类类型的智能指针?
- 如何在不使用指针的情况下将派生类的对象作为参数传递给基类中的函数?
- 指向基类派生类的 std::unique_ptr 的指针
- 当目标指针不是基类的类型时,为什么允许dynamic_cast为多态类生成 null 指针?
- 如何在成为指向基类的指针后保留对子类方法的使用?
- 派生类是从基类继承 v 指针并仅使用它,还是也有自己的 v 指针?
- std::variant vs指向C++中异构容器基类的指针
- 模板化函数或具有指向基类的指针的函数
- 指向基类的唯一指针
- C++函数解析基类指针到派生类指针
- 指向基类的指针的 std::vector 的深层副本
- 在将派生类指针类型转换为派生类指针后,从基类指针调用派生类函数
- 如何将子类作为函数的参数传递给期望基类,然后将该对象传递到指向这些抽象类对象的指针向量中?
- 无法从指针派生类转换为指针基类(多态性)
- 使用指针基类的=操作符的多态性