wize1
November 17, 2018, 9:30am
1
Hi guys,
I'm stuck on a bit of problem I hope someone can help me with...
I'm trying to pass callback to Timer class from another class and getting following errors:
in syntax error list:
argument of type "void (Test::)()" is incompatible with parameter of type "void ( )(void *)"
in build:
Test.cpp: In member function void Test::SetTimer()
Test.cpp: 13:30: error: invalid use of non-static member function
_timer.after(1000, _callBack)
Error compiling project sources
and this is my test code:
Test.h:
#pragma once
#include "Timer.h"
class Test
{
public:
Test();
~Test();
void SetTimer();
private:
Timer _timer;
void _callBack();
};
Test.cpp:
#include "Test.h"
Test::Test()
{
}
Test::~Test()
{
}
void Test::SetTimer()
{
_timer.after(1000, _callBack); // <---- PROBLEM LINE
}
void Test::_callBack()
{
// do something here
}
How can i pass class method as callback function to Timer?
PaulRB
November 17, 2018, 10:05am
2
Is there more code that you haven't posted? Like a .ino where you create a "Test" object?
wize1
November 17, 2018, 10:14am
3
PaulRB:
Is there more code that you haven't posted? Like a .ino where you create a "Test" object?
Test.ino:
#include "Test.h"
Test _test;
void setup()
{
_test.SetTimer();
}
void loop()
{
}
PaulRB
November 17, 2018, 10:25am
4
This is at the edge of my C++ knowledge. I need to improve...
Have at look at how the Timer library does it and compare that with your code.
wize1
November 17, 2018, 10:50am
5
It is same Timer library i was referring to, the problem is passing callback to it from class rather then from main loop function.
gfvalvo
November 17, 2018, 12:48pm
6
Short Answer - because of the way instance methods are implemented, you can't pass a pointer to one as you would a pointer to a regular function. If you are only going to need one instance of the Test class, then you can use a static member function. See discussion here: Using an Interrupt from a method inside a class - Programming Questions - Arduino Forum