简体   繁体   English

传递派生类指针进行回调

[英]Passing a derived-class pointer for a callback

I'm trying to build a callback-based serial RX driver, and am getting a compile error despite the fact that I'm using a structure which is known good and tested in other applications. 我正在尝试构建基于回调的串行RX驱动程序,尽管我使用的结构良好,并且在其他应用程序中经过测试,却遇到编译错误。 I'm sure I'm doing something silly, but for the life of me I can't see it, so a few extra pairs of eyes would be much appreciated. 我确定我做的事很愚蠢,但是对于我一生来说,我看不到它,所以多欣赏几双眼睛会倍感赞赏。

The device should function as follows: 该设备应具有以下功能:

  1. On construction, pass in the comms properties 在施工中,传递comms属性
  2. For synchronous communications, do not create a new thread, just block on the TX. 对于同步通信,不要创建新线程,只需在TX上阻塞即可。
  3. For receiving data, spawn a new thread, and execute a callback passed in by the user on RX. 为了接收数据,请生成一个新线程,然后执行用户在RX上传递的回调。

To implement this, I have a base callback class as follows: 为了实现这一点,我有一个基本的回调类,如下所示:

namespace ocular
{
 Class DeviceCallback
 {
 public:
   DeviceCallback(){}
   ~DeviceCallback(){}
   virtual void DeviceCallbackFunction(unsigned char Data){}
 };
}

And the Start Asynch RX method within the device class itself as: 设备类本身内的Start Asynch RX方法为:

void DeviceClass::StartAsynchRX( DeviceCallback* callback )
{
  m_externalCallback = callback; // Save a local copy of the callback pointer
  m_started = true;
  StartAsynchRXThread();         // Spawn the RX Thread
  return;
}

I am then deriving my own callback as: 然后,我将派生自己的回调为:

Class DemoCallbackClass : public ocular::DeviceCallback
{
public:
  void DeviceCallbackFunction(unsigned char myData){
    std::cout << myData;
  };
}

As far as I can tell, this is textbook correct thus-far. 据我所知,到目前为止这是正确的教科书。 This will compile just fine and I can construct, configure and use my device class for synchronous TX. 这样可以很好地编译,并且我可以构造,配置和使用我的设备类进行同步TX。 The compile error appears when I attempt to actually start passing the callback pointer in main: 当我尝试实际开始在main中传递回调指针时,出现编译错误:

void main(void)
{
  DeviceClass MyDevice();
  MyDevice.Initialise( *settings from file* );

  DemoCallbackClass MyDemoCallback();

  MyDevice.StartAsynchRX( &MyDemoCallback );  // ERROR ON THIS LINE
}

1>......\\Src\\Support\\AR2500 Commissioning\\Main.cpp(99): error C2664: 'ocular::DeviceClass::StartAsynchRX' : cannot convert parameter 1 from 'DemoCallbackClass (__cdecl *)(void)' to 'ocular::DeviceCallback *' 1> ...... \\ Src \\ Support \\ AR2500调试\\ Main.cpp(99):错误C2664:'ocular :: DeviceClass :: StartAsynchRX':无法从'DemoCallbackClass(__cdecl *)(void)转换参数1 '到'ocular :: DeviceCallback *'

I'm reasonably sure I've done something silly here, but I can't find it for the life of me. 我可以肯定地说我在这里做了一些愚蠢的事情,但是我一生都找不到。 Last week I wrote an event timer class using exactly the same approach and it works just fine. 上周,我使用完全相同的方法编写了一个事件计时器类,并且工作正常。 If the structure/approach looks OK to everyone out there, I guess It's a subtle typo, and I just need to rewrite a chunk at a time until I get rid of the damn error. 如果结构/方法对周围的每个人看起来都不错,那么我猜这是一个细微的错字,我只需要一次重写一个块,直到摆脱该死的错误。

Yours in desparation, 你在绝望中

DKW 大客车

Remove the parenthesis from this line: 从此行删除括号:

DemoCallbackClass MyDemoCallback();

So it looks like this: 所以看起来像这样:

DemoCallbackClass MyDemoCallback;

The error output is not helpful at all in this case, but that should fix it. 在这种情况下,错误输出根本没有帮助,但这应该可以解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 传递派生类指针C ++时发生运行时错误 - Run-time error when passing derived-class pointer C++ 指向派生 class 的基类指针无法访问派生类方法 - Base-class pointer pointing to a derived class cannot access derived-class method 如果超级构造函数不使用该指针,是否可以将指向派生类字段的指针传递给超级构造函数? - Can I pass a pointer to a derived-class field to the superconstructor if the superconstructor doesn't use that pointer? 如何实现boost :: variant派生类? - How to implement a boost::variant derived-class? 为什么不调用函数的派生类版本? - Why isn't the derived-class version of the function being called? 向下转换后的 C++ 派生类成员 - C++ derived-class members after downcasting 将指向一个派生类的指针传递给另一个派生类 - Passing a pointer to one derived class to another 将指针传递给派生类,传递给希望指向基类的指针的函数? - Passing pointer to derived class, to function that expects pointer to base class? C ++-传递智能指针派生类 - C++ - passing smart pointer derived class 将指针传递给派生类会导致编译器错误 - Passing a pointer to a derived class gives compiler error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM