I need to use a software timer within a class but I cannot get the code to compile.
The should work _updateTimer = new Timer(4, &myClass::update, this); as it follows the same syntax as Particle.function("control", &myClass::control, this); which does compile.
I read some other posts on the forum about using lambda to solve this issue but it seems like it should be unneccessary, plus I could not get a lambda to compile.
How should I be instantiating and using a software timer within a class so that it calls a function: void myClass::update() {...}
If you called your class MyClass…
In your header file (.h), usually in Private.
Timer* timerObj;
In Class begin() function (it is my understanding this won’t work in you class constructor because you can’t guarantee in what order objects will be instantiated.
timerObj = new Timer(50, (void (*)())&myClass::update);
To start and stop your timer (any Timer Class member function actually)
I have tried the syntax listed here, but I cannot get this to compile? I get;
no matching function for call to 'Timer::Timer(int, void (Foo::*)(), Foo&)
The code in the header file is;
Class Foo{
void onTimeout();
_Timer *myTimer;
public:
Foo(int x, int y, int z);
}
and in the cpp;
Foo::Foo(int x, int y, int z){
myTimer = new Timer (x, (void (*)())&Foo::onTimeout);
}
I’m really trying to get my head around this but I am struggling with the class member functions! Any help would be appreciated!
Also, I have tried the code from this thread as well.
And both will not compile.
I am working on an electron project and incidentally I tried compiling for a photon and it worked???
Maybe calling member class functions is not supported on the electron?
You haven’t marked the class method static.
When you want to pass a method to a function that is ignorant of the object instances the method needs to be static to be resolved by the compiler.
To have non-static functions play well, the receiving function needs to learn about the instance pointer of the respective object too.
Thanks @ScruffR
I gave that a crack but I still get an error;
The error seems to be the ‘this’ so maybe the timers are not allowed to be class members?
I have tried the code from the docs, and that works fine as a non class member but it doesn’t work as soon as it is a class member.with the ‘this’.