Hi everyone, I’m fairly new to C and C++, and particularly PROS, and I seem to have run into somewhat of a brick wall while trying to add an abstraction layer between the logic and hardware within my team’s code. My idea is to have a drivetrain class containing all the functions required for autonomous driving (drive, strafe, turn, etc.), as well as a completely self-contained controller loop. In order for the program to continue after the controller loop has started, I need to run the loop in a separate thread, which I believe should be done using pros::Task. Because the controller loop requires the accessing of a lot of private variables, I need it to be a member function of the class. However, when instantiating a Task with such a member variable, I get a number of errors, which are (unfortunately) above my limited debugging skills.
Here is a minimal reproducible example of my code:
#include "main.h"
#include "string"
class DriveTrain
{
private:
std::string helloWorld {"Hello, World!"};
public:
DriveTrain()
{
pros::Task helloWorldTask(helloWorldPrint);
}
void helloWorldPrint(void * param) {
std::cout << helloWorld << std::endl;
}
};
void initialize() {
DriveTrain base;
}
This produces the following error:
Compiled src/main.cpp [WARNINGS]
src/main.cpp: In constructor 'DriveTrain::DriveTrain()':
src/main.cpp:11:35: error: invalid use of non-static member function 'void DriveTrain::helloWorldPrint(void*)'
11 | pros::Task helloWorldTask(helloWorldPrint);
| ^~~~~~~~~~~~~~~
src/main.cpp:14:10: note: declared here
14 | void helloWorldPrint(void * param) {
| ^~~~~~~~~~~~~~~
Adding timestamp [OK]
Linking hot project with ./bin/cold.package.elf and libc,libm,libpros,okapilib [OK]
Section sizes:
text data bss total hex filename
540.00B 4.00B 46.02MB 46.02MB 2e03a21 bin/hot.package.elf
Creating bin/hot.package.bin for VEX EDR V5 [DONE]
Capturing metadata for PROS Editor...
When adding a static keyword to the front of the helloWorldPrint
function, I instead get the following error:
Compiled src/main.cpp [WARNINGS]
src/main.cpp: In static member function 'static void DriveTrain::helloWorldPrint(void*)':
src/main.cpp:15:22: error: invalid use of member 'DriveTrain::helloWorld' in static member function
15 | std::cout << helloWorld << std::endl;
| ^~~~~~~~~~
src/main.cpp:7:17: note: declared here
7 | std::string helloWorld {"Hello, World!"};
| ^~~~~~~~~~
Adding timestamp [OK]
Linking hot project with ./bin/cold.package.elf and libc,libm,libpros,okapilib [OK]
Section sizes:
text data bss total hex filename
540.00B 4.00B 46.02MB 46.02MB 2e03a21 bin/hot.package.elf
Creating bin/hot.package.bin for VEX EDR V5 [DONE]
Capturing metadata for PROS Editor...
Sorry about the long post, any help is much appreciated. Thanks!