2011-10-03 125 views
2

我試圖創建父類型i_MessageHandler的CnD_Message_Handler。 i_MessageHandler構造函數接受另一個抽象類i_MessageFactory。 CnD_Message_Factory繼承自i_MessageFactory。當我嘗試實例化CnD_Message_Handler,我收到以下錯誤:將對象傳遞給C++中的抽象類型構造函數

錯誤C2664:「CnD_Message_Handler :: CnD_Message_Handler」:無法從「CnD_Message_Factory」轉換參數1「常量CnD_Message_Handler &」 原因:不能從轉換' 'CnD_Message_Factory'到'const CnD_Message_Handler'

從網上的例子,我相信我正確地通過msg_factory。我也混淆爲構造函數請求i_MessageFactory(CnD_Message_Factory)而不是i_MessageHandler(CnD_Message_Handler)

感謝您的任何幫助提前!

CnD_Device(其實例CnD_Message_Factory和CnD_Message_Handler)

CnD_Device::CnD_Device(void) 
{ 
    CnD_Message_Factory msg_factory;     //Inherited by i_MessageFactory 
    CnD_Message_Handler msg_handler(msg_factory); 
} 

CnD_Message_Factory

#include "i_messagefactory.h" 

    class CnD_Message_Factory : 
     public i_MessageFactory 
    { 
    public: 
     CnD_Message_Factory(void); 
     ~CnD_Message_Factory(void); 

     /** 
     * Creates a message using the stream of data passed in. 
     * @param id Id of the message to create. 
     * @param stream Data stream to create the message from. 
     * @return The created message (which must be returned to the factory by 
     * calling the deleteMessage() method, or null if the factory could not 
     * create a message. 
     */ 
     Message* createMessage(UInt32 id, const char* stream); 

     /** 
     * Returns a message to the factory for deleting/recycling. 
     * @param msg The message being returned. 
     */ 
     void deleteMessage(Message& msg); 
    }; 

CnD_Message_Handler

#include "i_messagehandler.h" 

class CnD_Message_Handler : 
    public i_MessageHandler 
{ 
public: 


    CnD_Message_Handler::~CnD_Message_Handler(void); 

/** 
* Called by a i_MessageDriver object to process a message received. 
* @param msg Message to process. 
*/ 
void CnD_Message_Handler::handleMessage (Message& msg); 

/** 
* Called by a i_MessageDriver object when an error occurs with an 
* interface The exact type of errors are driver specific. 
* @param error The error that occurred. 
*/ 
void CnD_Message_Handler::handleError (MessageEvent& error); 

/** 
* Called by the i_MessageDriver object when an event occurs with an 
* interface. The exact type of events are driver specific. 
* @param event The event that occurred. 
*/ 
void CnD_Message_Handler::handleEvent (MessageEvent& event); 
}; 

i_MessageHandler

class i_MessageFactory 
{ 
    public: 

    /** 
    * Destructor. 
    */ 
    virtual ~i_MessageFactory(void) { } 

    /** 
    * Creates a message using the stream of data passed in. 
    * @param id Id of the message to create. 
    * @param stream Data stream to create the message from. 
    * @return The created message (which must be returned to the factory by 
    * calling the deleteMessage() method, or null if the factory could not 
    * create a message. 
    */ 
    virtual Message* createMessage(UInt32 id, const char* stream) = 0; 

    /** 
    * Returns a message to the factory for deleting/recycling. 
    * @param msg The message being returned. 
    */ 
    virtual void deleteMessage(Message& msg) = 0; 


    protected: 

    /** 
    * Constructor. 
    */ 
    i_MessageFactory(void) { } 
}; 
+0

修復,如果你想任何幫助你的代碼格式。 – Macke

+0

對不起,我的代碼格式有什麼問題?我應該發佈我的所有代碼嗎? – Kat

+0

您可能會嘗試在單獨的cpp中創建問題示例 - 在您的項目之外。看看你是否可以將問題歸結爲一些基本代碼。因爲我一直在沙箱項目中根據自己提供的內容玩弄不同的排列組合,並且無法讓您的錯誤發生。 –

回答

2

CnD_Message_Handler does not redefine the constructor.

構造函數在C++ 03中不是「繼承」的。您需要爲您繼承的所有類型提供構造函數參數。這是一個例子。

struct Arg {}; 

struct Foo { 
    Foo(Arg arg) {} 
    virtual ~Foo() {} 
}; 

struct Bar : public Foo { 
    Bar(Arg arg) : Foo(arg) {} 
}; 

它們可以繼承C++ 11,但需要特殊的語法。

struct Bar : public Foo { 
    using Foo::Foo; 
}; 
+0

謝謝。我在假設構造函數被繼承。那是我的問題。 – Kat

0

什麼是由CnD_Message_Handler定義的構造函數?您需要一個接受(引用)i_MessageFactory(或CnD_Message_Factory)的引用。如果不是,它會嘗試自動生成的構造函數,如複製構造函數。我認爲這是發生在這裏的事情。

1

CnD_Message_Handler沒有用戶定義的構造函數。相反,它嘗試使用編譯器免費提供的拷貝構造函數,並告訴它它不能將您傳入的工廠轉換爲編譯器提供的拷貝構造函數期望的const CnD_Message_Handler&

簡單地定義一個構造CnD_Message_Handler採取工廠和實例化它的基類:

CnD_Message_Handler(i_MessageFactory& foo) : i_MessageHandler(foo) {} 
相關問題