"..\CWatchDog.cpp", line 107: error #4167: argument of type
"void (CWatchDog::*)()" is incompatible with parameter of type
"FUNCPTR"
wdStart(m_WatchDogID, CYCLE_TIME, resetWatchDogTimer,0);
^
1 error detected in the compilation of "..\CWatchDog.cpp".
make: *** [CWatchDog.o] Error 0x1
According to section 7.2.1 of the Programmers guide, to make C++ accessible
to C code, I need to give my C++ function C linkage by prototyping it using
extern "C". This I have done, but I'm still doing something wrong.
Here's my code in CWatchDog.cpp
// My Prototype
extern "C" void resetWatchDogTimer();
// My instruction in a method in CWatchDog.cpp
WDOG_ID m_WatchDogID = wdCreate();
wdStart(m_WatchDogID, CYCLE_TIME, resetWatchDogTimer,0);
// And the method that I wish to attach to the timer
void CWatchDog::resetWatchDogTimer()
{
// do something
}
Can anyone offer advice on how to pass the C++ method to the wdStart()
function?
Thanks in advance to any help you can offer.
Also note, that if your function is accessing other members of the class,
you will have to find another way to do it after you make it static.
Regards,
DrX
I had the same problem with taskSpawn some weeks ago : use a C wrapper
function as WD routine, and pass it a pointer to your CWatchDog
instance as parameter to wdstart, something like that :
void Cwrap( int xx )
{
CWatchDog *wd = (CWatchDog *)xx;
wd->resetWatchDogTimer();
}
...
wd = new CWatchDog(...);
...
wdStart(m_WatchDogID, CYCLE_TIME, Cwrap, (int)&wd);
I wrapped the method in a global C function so I can access the
non-static members of the class.
Regards,
David
"Xenos" <dont.s...@spamhate.com> wrote in message news:<cbusif$mt...@cui1.lmms.lmco.com>...
DrX
"David" <ma...@david-eng.com> wrote in message
news:ddf6334f.04070...@posting.google.com...
Please post an example if you can. I think I can figure it out, but just in case.
Regards,
David
"Xenos" <dont.s...@spamhate.com> wrote in message news:<cc167j$mt...@cui1.lmms.lmco.com>...
That is what I ended up doing.
Regards,
David
aldr...@hotmail.com (aldrin19) wrote in message news:<380e665d.04070...@posting.google.com>...
class W {
public:
W() {id = ::wdCreate();}
void start(int delay)
{::wdStart(id, delay, reinterpret_cast<FUNCPTR>(wrapper),
reinterpret_cast<int>(this));}
void stop() {::wdCancel(id);}
protected:
void func() {}
private:
WDOG_ID id;
static void wrapper(W* self) {self->func();}
};
This makes the entire class self-contained. Some considerations:
1. If you make func() virtual, you can create subclasses and overrride the
behavior for different watchdogs. If you do this, make sure to also make
the destructor virtual.
2. Be very careful about owership. For example, if you decide to put the
wdDelete() function in the descructor, and you copy the object, you will be
deleting it twice (be careful of temporaries!). Either give up the
ownership to the new object in you copy constructor and assignment operator,
disallow these functions, or use reference counting to track ownership.
3. Make sure the object "lives" for at least as long as the watch dog is
active. You don't want the watchdog calling functionality for a class
object that has been deconstructed.
4. This kind of trick will work with any library API that allows the client
software to give it a user defined parameter for the provided function
(i.e., taskSpawn).
How this helps,
DrX
I now understand how to deal with these type of functions.
Regards,
David
"Xenos" <dont.s...@spamhate.com> wrote in message news:<ccgqlr$3s...@cui1.lmms.lmco.com>...