How would I go about using #include to include other header or source files in my VEXcode project? When I make a new source file and include in it vex.h (so it can access my motor ports) and then try to include that file in main.cpp it gives me errors like "you are redefining “Controller 1” and stuff like that. I am trying to put all of my autonomous routines in separate files.
If I was to put an autonomous routine in a separate file should it be a .h or a .cpp file and what directory should I put it in?
In cpp (that’s what VEXcode uses) #include includes other source file into your program. You can imagine it as coping and pasting the file right where the include statement is. So first off the .h and .cpp endings don’t actually have to be used, it is just a convention everyone uses but it makes things easier to understand so please use it. The .h is header files (other link), which should contain declarations, which, for example, give a function name and it’s parameters. The function body is then declared in the .cpp.
This would look something like this...
It is also convention to name the files the same name…
stuffClass.h
#pragma once //I'll explain this later
int myAwesomeFunction(int number); //declaration
class stuff
{
public:
int myAwesomeFunction2(int number); //declaration
};
stuffClass.cpp
#include "stuffClass.h"
int myAwesomeFunction(int number) //definition
{
return number + 1;
}
int stuff::myAwesomeFunction2(int number) //definition
{
return number + 2;
}
The reason it is done like this is to be able to look at a header file and see all the function/class/variables without lots of code making things more confusing.
Also only header files ever need to be included.
The problem you are having is that you are declaring the same thing multiple time. In main.cpp you include vex.h which already includes your autonomous functions. Then you included your autonomous function again in main.cpp.
First you don’t need to include it in the main.cpp since it is already included in vex.h. The error should go away at this point, but you are not done. To avoid the problem of redefining anything something called include guards are used. They are necessary for more complex program where you have multiple includes of the same file, which technicaly aren’t necessary, but make life easier. Never the less you should just use them. The simplest way to do this is using #pragma once.
This is completely up to you and anything would work.
I would probably separate them into .h and .cpp files if you have many/multiple functions so that you can see them all in a small file. If you are planning on have just one function it would probably be easier to just have one .cpp file (you can still use #program once) and include it in vex.h.
Also to access your motor ports (which I assume you declared in vex.h) you should include vex.h in the autonomous routine file(s).