I’m trying to access functions from another file for use inside my class definition:
// math.cpp
int Sum(int a, int b){
return (a + b);
}
// my_class.cpp
#include <math.cpp>
#include <my_class.h>
int ComputeSomething() {
...
return ::Sum(num1, num2);
}
Despite my best efforts, I can’t get the compiler to spit out anything outside the likes of ::Sum has not been declared or Sum was not declared in this scope.
I’m trying to wrap my head around code organization in C++, any help appreciated.
It might be worth noting that I’m programming for Arduino.
To be able to access functions from a user-defined library, best divide that library into a .h (or .hpp) and a .cpp file. I understand you have actually done this, but tried various options – among them the inclusion of the .cpp file – for the sake of finding a solution.
Still, to ensure things work as expected, the declarations of functions and classes should go into the .h file, best protected by something like
Then to include the .h file (I’ll assume it’s named my.h), either use
or
The latter only works if my.h is found on an include path previously known to the compiler (e.g. what is specified using the
-Icommand line option in GCC). The former works if the path to the .h file given is relative to the directory your are building from.Finally, do not use a file name that can be confused with a system library (such as “math.h”), especially if you are using the
<...>syntax, as the include path will definitely include the system library header files.