简体   繁体   中英

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. 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
  2. For synchronous communications, do not create a new thread, just block on the TX.
  3. For receiving data, spawn a new thread, and execute a callback passed in by the user on 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:

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. The compile error appears when I attempt to actually start passing the callback pointer in 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 *'

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM