How to fix "No matching constructor for initialization of 'vex::task'" error?

We’re trying to make a PID function, but there is an error that pops up. When we hover over the red text, the pop-up box says:

[ clang] No matching constructor for initialization of ‘vex::task’

  • vex_task.h(32, 9): Candidate constructor (the implicit copy constructor) not viable: no known conversion from ‘void ()’ to ‘const vex::task’ for 1st argument
  • vex_task.h(54, 7): Candidate constructor not viable: no known conversion from ‘void ()’ to ‘int (*)()’ for 1st argument
  • vex_task.h(46, 7): Candidate constructor not viable: requires 0 arguments, but 1 was provided
  • vex_task.h(63, 7): Candidate constructor not viable: requires 2 arguments, but 1 was provided
  • vex_task.h(73, 7): Candidate constructor not viable: requires 2 arguments, but 1 was provided
  • vex_task.h(83, 7): Candidate constructor not viable: requires 3 arguments, but 1 was provided

Declared in function main
class vex::task trackPositionTask

We’re not sure what to do to fix this, so any help would be appreciated, thank you!
image

Welcome (back) to the forum!

There are 2 main ways of fixing this problem.

1. ‘Handlebar’ task method
This task method will allow you to stop the task at any point in your code.
If you want to use this way, follow the formatting below. Place all the code you want to run in tempTASK and call the task using temp. If you need to pass any variables into the task, make a global variable (a variable outside of each of the task functions) and grab the values from there.

int hello = 0;
int tempTASK(){
// to pass arguments into your task, use a global variable.
hello++;
return 0;
}

vex::task temp(int helloParameter) {
hello = helloParameter;
return vex::task (tempTASK);
}

// this way is useful because you can do this and kill the task whenever you want!
vex::task e = temp(4);
e.stop();

You can copy and paste this into your code and change the function names/parameters/actions but make sure that if you change a name that it is changed everywhere else it is used. The compiler will help you with this, it’ll let you know if there are any errors.

2. Regular task method
This task method is what my team used before discovering the handlebar method. It works very well and is slightly less difficult to implement than the previous method, but you can’t stop it at any point in the code.

int hello = 0;
int tempTASK(){
// to pass arguments into your task, use a global variable.
hello++;
return 0;
}

void temp(int helloParameter) {
hello = helloParameter;
vex::task t(tempTASK);
}

Same with the other task method, you can change any names you like as long as you change all the places where that name is used.

Information that pertains to both task methods
You can call both of these as many times as you like. Just use temp(parameters); to call it, changing parameters for whatever parameters you are going to pass in.

Example: temp(4);

Hope that helped, and if you have any other questions, let me know! :grin:

4 Likes