I have incurred an issue when running my PROS program. I am new to pros so bear with me. Basically, I have a function in the opcontrol.cpp file for the drivetrain which assigns the velocity of the motors to a joystick value. In another file, which I named hardware_def.cpp, I have all the motor and controller definitions. I included both files. When I downloaded the program however, nothing happened. I don’t get any errors and it even detects when the competition switch is in driver control or autonomous.
I got this triple checked by some fellow programmers and they couldn’t even find the issue. I also coded a simple p-loop for autonomous function just to check if the controller was working or not, and that didn’t work either.
All the ports are correct, the motors just don’t move. Can someone double check my PROS src files on GitHub.com. I will provide the link here.
Also, in the autonomous.cpp file, I am getting in error where I call the “pLoop” method (towards the bottom of the file). It’s saying that it is an “undeclared identifier” even though I defined that method in the “PID_P-loop_IME.cpp” file and included it in the autonomous file. The parameters also align with the method so I’m not sure why I’m getting that error.
Your git doesn’t seem to have your header files (.h or .hpp). PROS for V5 is based off of C++. So you need to use header files to combine two .cpp files, and not by directly including the two .cpp files. If you want I can try and upload an example program in a bit.
Just curious, would you say header files in these cases are comparable to interfaces and/or abstract classes in java? I’m more familiar with java and I’m just trying to figure out when to implement a .hpp along with my .cpp. For example, would I, for my method library, create a .hpp to go with it wit public variables and abstract method headers? Will my method library not work otherwise?
Most of these responses do get to the core of the problem - in C++, you should never include source (cpp) files into each other.
The way the compiler works is (unlike RobotC) it compiles each source file individually and separately. Then, it merges all the compiled sections into one executable. If you include cpp files into each other, the final executable will contain duplicates of the functions and variables, because it compiled it multiple times in different locations.
For each file to be compiled separately without the rest of the code, you need to give it placeholders for variables in other files, via header files.
It is a good idea to read through http://learncpp.com/. https://www.learncpp.com/cpp-tutorial/forward-declarations/ https://www.learncpp.com/cpp-tutorial/programs-with-multiple-code-files/ https://www.learncpp.com/cpp-tutorial/header-files/
and S.4.1a - S.4.3a
Not exactly, but maybe depending on how you want to look at it. Think of header files as telling your source file “hey, these functions and variables exist and you can call/use these” and source files as “this is where those functions/variables actually exist.” Ish… It’s easy to make it more complicated and I like to favor a more fundamental understanding over technical precision.
As Theo and others have mentioned, source files are compiled separately and then linked together to create one executable. If you repeat the same variable/function names, the process of linking these files together fails - does the lEncoder refer to the variable in opcontrol.cpp or the one in autonomous.cpp? When you create a header file, you can tell the compiler for that source file that some variables or functions exist in the whole program, but won’t be created here.
So, you create a source file hardware_def.cpp that initializes your different Motors, ADIEncoders, etc. You create a hardware_def.hpp file that describes the existence of those variables in hardware_def.cpp. And then in opcontrol.cpp, etc. you can include that header file so that the compiler knows that those variables exist. @Codec did that for you.