【问题标题】:Passing in an Object to an abstract type's constructor in C++在 C++ 中将对象传递给抽象类型的构造函数
【发布时间】:2011-10-03 14:45:21
【问题描述】:

我正在尝试创建父类型 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) { }
};

【问题讨论】:

  • 如果您需要任何帮助,请修复您的代码格式。
  • 对不起,我的代码格式有什么问题?我应该发布所有代码吗?
  • 尝试在项目之外的单独 cpp 中创建问题示例可能对您有所帮助。看看您是否可以将问题具体化为一些基本代码。因为我一直在根据您提供的内容在沙盒项目中使用不同的排列,并且无法让您的错误发生。
  • @DougT。感谢您的建议。我将尝试在单独的沙箱中重现问题。

标签: c++ abstract-class c2664


【解决方案1】:

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;
};

【讨论】:

  • 谢谢。我假设构造函数是继承的。那是我的问题。
【解决方案2】:

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

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

CnD_Message_Handler(i_MessageFactory& foo) : i_MessageHandler(foo) {}

【讨论】:

    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 2021-05-08
      • 1970-01-01
      • 2014-06-04
      • 1970-01-01
      • 1970-01-01
      • 2021-11-24
      • 1970-01-01
      • 1970-01-01
      • 2011-10-24
      相关资源
      最近更新 更多