Using a user defined class method instead of a function in vex's task class

I was wondering if it is in any way possible to use a user defined class method in place of a function when creating a task (using vex’s built - in task class). I have tried already and got an error message, here is the code and error message:

#include "vex.h"
using namespace vex;

class PID
{ public:

  PID ()
  {
  }

  void Do_somthing ()
  {
  }

};

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  PID my_pid = PID();
  task my_task = task(my_pid.Do_somthing);

}

The error message I get (not during compiling) occurs on the line where I declare the task

task my_task = task(my_pid.Do_something) ;

This is the error message:

[clang] No matching conversion for functional-style cast from ‘void’ to ‘vex::task’

  • vex_task.h(32, 9): Candidate constructor (the implicit copy constructor) not viable: cannot convert argument of incomplete type ‘void’ to ‘const vex::task’ for 1st argument
  • vex_task.h(54, 7): Candidate constructor not viable: cannot convert argument of incomplete type ‘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

[clang] Reference to non-static member function must be called; did you mean to call it with no arguments? (fix available)

You didn’t follow the correct format for defining tasks. For your provided code, it should be
task my_task(my_pid.Do_something);
You should look into the API: Link to Tasks Specifically

2 Likes

I tried both your method and my method of defining a task with a function, and it works just fine (as a method of defining a task object), but the error message was still there (when I used yours and my method with a class method).

I think it has something to do with the fact that I was trying to use a method of a class as the parameter and not a function.

Now that I think about it though, I believe that I could accomplish what I was trying to accomplish with the class method with a plain old function anyway.

Anyway thanks for your help. (:

2 Likes

you would need to use a static class member function. See this, not exactly the same as what you are trying to do but similar concept.

8 Likes