03-31-2015 06:22 AM
Hello all,
I am trying to incorporate one of the ANSI C examples into my code which contains C++ classes. I receive errors because of having to declare the EveryNCallback and DoneCallback functions as static to keep their C function signature. This of course is not allowable when using these functions as members of a class. Could anyone help please?
Best,
Paolo
04-01-2015 07:38 AM
Hi Paolo,
Could you please give me the error code numbers you are receiving so I can look into the issue further?
I look forward to hearing from you.
Kind regards
Seb
04-04-2015 11:16 AM
"invalid use of member 'temperature::window' in static member function"
when I use window->"some function" this in the call back function. There is no error number.
04-07-2015 01:17 PM - edited 04-07-2015 01:18 PM
Hi Paolo,
http://isocpp.org/wiki/faq/pointers-to-members#memfnptr-vs-fnptr demonstrates how to use a C callback function to invoke a method on a C++ object.
Note that the DAQmx event functions have a "void *callbackData" parameter that you can use to pass the C++ object's "this" pointer to the static callback function. You can use static_cast to downcast the void* to your C++ class type before calling the method.
Brad
04-08-2015 11:07 AM
Firstly, thank you for the help. I have attempted to code the advice you gave but it still does not seem to compile properly. Can you suggest what is wrong with my code please?
Here is the code I am trying to configure:
forceDriver.h
static int32 CVICALLBACK static_callback(TaskHandle taskHandle, int32 everyNsamplesEventType, uInt32 nSamples, void* callbackData);
int32 EveryNCallback(TaskHandle taskHandle, int32 everyNsamplesEventType, uInt32 nSamples);
int32 DoneCallback(TaskHandle taskHandle, int32 status, void *callbackData);
forceDriver.cpp
DAQmxErrChk (DAQmxRegisterEveryNSamplesEvent(taskHandle,DAQmx_Val_Acquired_Into_Buffer,2500,0,&forceDriver::static_callback,panel));
**START TASK**
int32 forceDriver::EveryNCallback(TaskHandle taskHandle, int32 everyNsamplesEventType, uInt32 nSamples)
{
panel->(member_function);
}
int32 CVICALLBACK forceDriver::static_callback(TaskHandle taskHandle, int32 everyNsamplesEventType, uInt32 nSamples, void* callbackData)
{
forceDriver* static_object = static_cast<forceDriver*>(callbackData);
return static_object->EveryNCallback(taskHandle, everyNsamplesEventType, nSamples);
}
where panel is the object I have created.
04-08-2015 01:08 PM - edited 04-08-2015 01:09 PM
Hi Paolo,
> I have attempted to code the advice you gave but it still does not seem to compile properly.
Without the text of the compiler errors, I can only speculate as to what's wrong.
> Can you suggest what is wrong with my code please?
I think you're on the right track with forceDriver::static_callback(). It receives a void*, downcasts it into an instance of forceDriver*, and calls a method on it. I'm assuming that static_callback was declared inside the class body of forceDriver and the "panel" variable that you passed to DAQmxRegisterEveryNSamplesEvent() is a pointer to a forceDriver.
However, I don't understand what the body of forceDriver::EveryNCallback() is supposed to do. You're passing the DAQmx API a pointer to a C function (really a static member function) that invokes your member function, so you shouldn't have to dereference any pointers to member functions. I don't know whether "panel" is in scope but you should be able to use "this" instead, because forceDriver::EveryNCallback() is not static.
Brad
04-13-2015 03:22 PM - edited 04-13-2015 03:29 PM
Hi Brad,
Firstly, thanks for the help and explaining how it works. Is it possible to use the callbackData parameter to pass an object into the callback? panel for me is a pointer to an object from a class out of scope. Will this be a problem?
Best,
Paolo
04-13-2015 03:40 PM
> Is it possible to use the callbackData parameter to pass an object into the callback?
Yes, that's what it's there for.
> panel for me is a pointer to an object from a class out of scope. Will this be a problem?
As long as it's in scope when you pass it to the function, it should be fine. My comments about scope were mainly about the variables that weren't declared in the snippet of code that you posted.