C++ 将对象传递给抽象类型';C+中的s构造函数+;
我正在尝试创建父类型I\u MessageHandler的CnD\u Message\u处理程序。i_MessageHandler构造函数接受另一个抽象类i_MessageFactory。CnD_Message_工厂继承自i_MessageFactory。当我尝试实例化CnD_消息_处理程序时,出现以下错误: 错误C2664:'CnD_Message_Handler::CnD_Message_Handler':无法将参数1从'CnD_Message_Factory'转换为'const CnD_Message_Handler&' 原因:无法从“CnD\u消息\u工厂”转换为“const CnD\u消息\u处理程序” 从网上的例子来看,我相信我通过味精工厂是正确的。我还感到困惑,因为构造函数请求I_MessageFactory(CnD_Message_Factory)而不是I_MessageHandler(CnD_Message_Handler) 提前谢谢你的帮助 CnD_设备(实例化CnD_消息_工厂和CnD_消息_处理程序) CnD_信息_工厂C++ 将对象传递给抽象类型';C+中的s构造函数+;,c++,abstract-class,c2664,C++,Abstract Class,C2664,我正在尝试创建父类型I\u MessageHandler的CnD\u Message\u处理程序。i_MessageHandler构造函数接受另一个抽象类i_MessageFactory。CnD_Message_工厂继承自i_MessageFactory。当我尝试实例化CnD_消息_处理程序时,出现以下错误: 错误C2664:'CnD_Message_Handler::CnD_Message_Handler':无法将参数1从'CnD_Message_Factory'转换为'const CnD_M
#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_消息_处理程序
CnD_Device::CnD_Device(void)
{
CnD_Message_Factory msg_factory; //Inherited by i_MessageFactory
CnD_Message_Handler msg_handler( msg_factory );
}
#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);
};
我是个信息处理者
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) { }
};
CnD_消息_处理程序定义的构造函数是什么?您需要一个(引用)i_MessageFactory(或CnD_Message_Factory)的。如果没有,它将尝试自动生成的构造函数,比如复制构造函数。我想这就是这里发生的事情 CnD_消息_处理程序不重新定义构造函数 构造函数在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;
};
CnD\u Message\u处理程序
没有用户定义的构造函数。相反,它试图使用编译器免费提供的复制构造函数,并告诉您无法将传入的工厂转换为编译器提供的复制构造函数所期望的const CnD\u Message\u处理程序&
只需为CnD_Message_Handler
定义一个构造函数,以获取工厂并实例化其基类:
CnD_Message_Handler(i_MessageFactory& foo) : i_MessageHandler(foo) {}
如果需要帮助,请修复代码格式。对不起,我的代码格式有什么问题?我应该发布我所有的代码吗?尝试在单独的cpp中创建一个问题的示例可能会对您有所帮助——在您的项目之外。看看你是否能把问题具体化为一些基本代码。因为我一直在一个沙盒项目中根据你所给出的不同排列,不能让你的错误发生。@DougT。谢谢你的建议。我将尝试在单独的沙箱中重新创建问题。谢谢。我假设构造函数是继承的。那是我的问题。