2

我正在尝试创建父类型 i_MessageHandler 的 CnD_Message_Handler。i_MessageHandler 构造函数采用另一个抽象类 i_MessageFactory。CnD_Message_Factory 继承自 i_MessageFactory。当我尝试实例化 CnD_Message_Handler 时,出现以下错误:

错误 C2664:“CnD_Message_Handler::CnD_Message_Handler”:无法将参数 1 从“CnD_Message_Factory”转换为“const 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) { }
};
4

3 回答 3

2

CnD_Message_Handler 没有重新定义构造函数。

构造函数在 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;
};
于 2011-10-03T15:01:00.147 回答
1

CnD_Message_Handler没有用户定义的构造函数。相反,它试图使用编译器免费提供的复制构造函数,并告诉您它无法将您传入的工厂转换为const CnD_Message_Handler&编译器提供的复制构造函数所期望的工厂。

只需定义一个构造函数CnD_Message_Handler来获取工厂并实例化其基类:

CnD_Message_Handler(i_MessageFactory& foo) : i_MessageHandler(foo) {}
于 2011-10-03T15:01:22.420 回答
0

CnD_Message_Handler 定义的构造函数有哪些?您需要一个接受(引用)i_MessageFactory(或CnD_Message_Factory)的人。如果没有,它将尝试自动生成的构造函数,例如复制构造函数。我认为这就是这里正在发生的事情。

于 2011-10-03T14:55:29.490 回答