[英]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: 该设备应具有以下功能:
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.