I have a header file declaring a method, and a code file with the implementation. On compile, the compiler errors out with 'undefined reference to...'. If I copy the implementation of the method to the header, it then tells me the method is being redefined in the code file. Surely if it can find the method in the cpp file to call a duplicate, then it can use it. Since it can't be found, adding it to the header shouldn't cause a redefinition.
All headers are protected. In the code below, if I uncomment the header implementation, the method is redefined, if I leave it commented, the method is undefined. If I use only the header implementation, everything compiles fine but is bad architecture. The cpp file is being correctly copied to the output folder. Example code:
- test.ino
- math.h
- math.cpp
test.ino
#include "math.h"
void setup() {
int result = Math::Double((int)2);
}
void loop() {}
math.h
#ifndef MATH_H
#define MATH_H
namespace Math {
template <typename U>
U Double(U value);
// template <typename U>
// U Interpolate(U value) {
// return value * 2;
// }
}
#endif
math.cpp
#include "math.h"
template <typename U>
U Math::Double(U value) {
return value * 2;
}