How do I use includes?

How would I use includes?
I have a file name called “drivePID.cpp” and I included it within the main file

And as I can see, it works and doesn’t show errors iside of the main.cpp file:
Screenshot (771)
But, if I look into the drivePID.cpp file, it have a lot of errors and it won’t allow me to build the project.

How would I utilize multiple files without causing these errors?

your motor definitions are unknown to the drivePID file as well as all of the vex-defined units, so you need to include ‘vex.h’ and ‘robot-config.h’

1 Like

That seemed to have stopped most of the errors, but I seem to get this error still:

(Btw thank you so much for your time)

It looks like you have identical variables in your main and pid files. Do you have another pid function in your main file that’s double defining variables?

I am quite unsure and cannot find anything within the project file: (8.6 KB)

1 Like

Sorry, I forgot to mention to remove the include ‘drivePID.cpp’
to use the functions from the file you need to make the declaration that the functions exist elsewhere like so before using it: int drivePID();

Also, to use variables between files you need to tell the file to look for the variable elsewhere. This can be done in any of the header files that are included but I put it in robot-config.h since I was too lazy to make a new file. If you want to see how I did it you can look there. (It’s just extern type var;)

I had to play around with your code a bit to figure out what was wrong and I ended up inadvertently fixing it, here is what I had: (18.5 KB)


Thank you so much! Very appreciated, I’m trying to work on a tutorial on files and this is kinda my first time using them :slight_smile:

In general, you shouldn’t #include .cpp files. Instead, you should declare prototypes for functions and objects you need in a .h file, and include that .h file.