Pros multitasking clock in structure(class)

Here is my code:

struct Timer{
bool timerRunning = false;
int time = 0;
void Timer_Service(void* param){
timerRunning = true;
while(timerRunning){
wait(10);
time += 10;
}
}
void startTimer(){
pros::Task timerservice(Timer_Service,(void*)“PROS”);
}
void stopTimer(){
timerRunning = false;
time = 0;
}
};

the Timer_Service in the line pros::Task timerservice(Timer_Service,(void*)"PROS"); shows an error :

void Timer::Timer_Service(void *param)
no instance of constructor “pros::Task::Task” matches the argument list – argument types are: (void (void *param), void *)

How do i solve this?
Thanks for your kind help

Does PROS not let you use tasks in structures?

struct Timer{
bool timerRunning;
int time;
void Timer_Service(void* param){
timerRunning = true;
time = 0;
while(timerRunning){
wait(10);
time += 10;
}
}
void startTimer(){
Timer_Service((void*)“PROS”);
pros::Task timerservice(Timer_Service,(void*)“PROS”); //<- error here
}
void stopTimer(){
timerRunning = false;
}
int getTime(){
return time;
}
};

Why does regularly calling the function work but the Pros task constructor is not happy with it? what is wrong?

This is what’s wrong. Read it carefully. pros::Task::Task is the constructor for a pros::Task. Saying that no version of it matches the arguments that you’ve given it means that you’re using it wrong by not giving it the thing’s it’s expecting, or giving it things it’s not expecting. For instance, if we had this function: void func1(char* Arg1) {...} and we tried to use it like this: double var = 1.0; func1(&var); it would produce a similar error because we gave it a double pointer instead of a character pointer. You need to go reread the documentation and figure out what the arguments should actually be.

1 Like

Yes i have to give it a function pointe, not the function name

The issue is probably related to the fact that class member methods generally cannot be used as callbacks from C functions. The same issue applies to the thread management we use in VEXcode. So usually what’s done is that you need to make the task a static method and pass a copy of the class instance as a parameter.
There’s some background on the issue here (and I’m not suggesting you use their solution)
http://p-nand-q.com/programming/cplusplus/using_member_functions_with_c_function_pointers.html

I will try and find time to look at your code in PROS next week.

I already solved it, thanks. Here is the code if you want to know

struct Timer
{
bool timerRunning;
int time;
static void Timer_Service_Delegate(void* param) {
Timer* ptr = reinterpret_cast<Timer*>(param);
ptr->Timer_Service();
}
void Timer_Service() {
timerRunning = true;
time = 0;
while(timerRunning){
wait(10);
time += 10;
}
}
void startTimer() {
pros::Task timerservice(
Timer_Service_Delegate,
reinterpret_cast<void*>(this)
);
}
void stopTimer() {
timerRunning = false;
}
int getTime() {
return time;
}
};

Thanks alot for your help. Im supposed to give it a function pointer not a function name.