2016-04-01 12 views
0

私は上記の問題を抱えていますが、これは前に動作するために使用されていたため、原因ははっきりとわかりました。私のプロジェクトの前方宣言。ポインタの派生クラスからポインタの基本クラス(多態性)に変換できません

私はいくつかのクラスを持っていますが、いずれもQObjectsUIElementInterfaceから派生しています。次の例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 

XmlReaderにおいてUIElementInterface

#ifndef UIELEMENTINTERFACE_H 
#define UIELEMENTINTERFACE_H 

class UIElementInterface 
{ 
public: 
    virtual ~UIElementInterface(){} 
    UIElementInterface(const QString& define, const QString& header); 
    //...   
}; 
#endif 

でIは、順方向CheckBox(及びUIElementInterface)を宣言しました。ここで私は、両方の適切な初期化を確認するために、いくつかのCheckBoxにおける印刷とUIElementInterfaceコンストラクタをした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*渡します。ここで

setUIElementの実装です:XmlReaderからこれを呼び出す

void CentralWidget::setUIElement(const QString& define, UIElementInterface* UIElement) 
{ 
    m_uiElementMap[define] = UIElement; 
} 

は私を与える:

UIElementInterfaceCheckBoxから
error: no matching function for call to 'CentralWidget::setUIElement(QString, CheckBox*&)' 

static_castinvalid static_castエラーを与えるだろうし、暗黙のキャストが私に与えますcannot convert ... in initializationエラー。

私が言ったように、同じ継承パターンが仕事に使用され、私が知っている限り、それは働くことを意味します。だから: 私は何が欠けていますか?

ありがとうございます。ここでEDIT

は(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 
` 
+0

*完全な*エラーメッセージを入力してください、それはしばしば有用である余分な情報が含まれています。 –

+0

'CentralWidget'のクラス定義と、' XmlReader'で宣言されているところがあれば役に立ちます。 – TriskalJM

+0

これを今質問に追加します。 – tobilocker

答えて

0

ターンのクラス定義m_centralWidget->setUIElement(getDefine(attr), checkBox);

m_centralWidget->setUIElement(getDefine(attr), (UIElementInterface*)checkBox);

+0

これは解決しませんか? 'reinterpret_cast'に? – Rotem

+0

こんにちは。これは前にキャストを必要とせず、アフリカンは設計上必要ではありませんでした。エラーは、私のfwdの宣言とインクルードパターンにネストされているようです。 – tobilocker

+0

@tobilocker宣言継承を転送することはできません。コンパイラは 'CheckBox'がフォワード宣言に基づいて' UIElementInterface'から継承することを知る方法がありません。 – Rotem

関連する問題