我試圖創建父類型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) { }
};
修復,如果你想任何幫助你的代碼格式。 – Macke
對不起,我的代碼格式有什麼問題?我應該發佈我的所有代碼嗎? – Kat
您可能會嘗試在單獨的cpp中創建問題示例 - 在您的項目之外。看看你是否可以將問題歸結爲一些基本代碼。因爲我一直在沙箱項目中根據自己提供的內容玩弄不同的排列組合,並且無法讓您的錯誤發生。 –