在 C 中将对象传递给抽象类型的构造函数;

发布于 2024-12-08 08:15:42 字数 3421 浏览 0 评论 0原文

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

I'm trying to create CnD_Message_Handler of parent type i_MessageHandler. i_MessageHandler constructor takes a i_MessageFactory, another abstract class. CnD_Message_Factory inherits from i_MessageFactory. When I try to instantiate the CnD_Message_Handler, I get the following error:

error C2664: 'CnD_Message_Handler::CnD_Message_Handler' : cannot convert parameter 1 from 'CnD_Message_Factory' to 'const CnD_Message_Handler &'
Reason: cannot convert from 'CnD_Message_Factory' to 'const CnD_Message_Handler'

From examples online, I believe I'm passing msg_factory correctly. I'm also confused as the constructor requests i_MessageFactory(CnD_Message_Factory) instead of i_MessageHandler(CnD_Message_Handler)

Thanks for any help in advance!

CnD_Device (which instantiates CnD_Message_Factory and 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) { }
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

千と千尋 2024-12-15 08:15:42

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

CnD_Message_Handler does not redefine the constructor.

Constructors are not "inherited" in C++03. You are required to provide constructor arguments for all types you inherit from. Here is an example.

struct Arg {};

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

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

They can be inherited C++11, but require special syntax.

struct Bar : public Foo {
  using Foo::Foo;
};
千仐 2024-12-15 08:15:42

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

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

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

CnD_Message_Handler has no user defined constructors. Instead its trying to use the copy constructor that the compiler gives you for free, and its telling you it can't convert the factory you passed in to the const CnD_Message_Handler& that the compiler-provided copy constructor expects.

Simply define a constructor for CnD_Message_Handler to take a factory and instantiate its base class:

CnD_Message_Handler(i_MessageFactory& foo) : i_MessageHandler(foo) {}
猫弦 2024-12-15 08:15:42

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

What are the constructors defined by CnD_Message_Handler? You need one that takes a (reference to a) i_MessageFactory (or CnD_Message_Factory). If not, it'll try the constructors generated automagically, like the copy constructor. And I think that is what's happening here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文