I’m just starting with C++ and PlatformIO. This is my first attempt to split code out of main.cpp into other .cpp and .h files.
Having read here that practically any guide on splitting C++ files should suffice, I’ve started with this one.
Here’s my header file (incidentally, I’ve tried building with and without the directives, with the same results).
#ifndef WAVES_H
#define WAVES_H
class Interpolator
{
public:
double Interpolate(double start, double end, double offset);
};
class InterpolatorTester
{
public:
void Test();
};
#endif
Here’s the implementation:
#include <waves.h>
double Interpolator::Interpolate(double start, double end, double offset)
{
return start + ((end - start) * offset);
}
void InterpolatorTester::Test()
{
Interpolator interpolator;
double result1 = interpolator.Interpolate(0, 100, 0.9);
if (result1 != 90.0){
throw (9001);
}
}
Here’s the build error
Compiling .pio/build/esp32dev/src/waves.cpp.o
src/waves.cpp: In member function 'void InterpolatorTester::Test()':
src/waves.cpp:11:58: error: 'double Interpolator::Interpolate(double, double, double)' is private within this context
double result1 = interpolator.Interpolate(0, 100, 0.9);
^
src/waves.cpp:3:8: note: declared private here
double Interpolator::Interpolate(double start, double end, double offset)
^~~~~~~~~~~~
*** [.pio/build/esp32dev/src/waves.cpp.o] Error 1
I don’t understand why the compiler thinks the Interpolate
method is declared private. The declaration defines it as public, and the place in the code referenced by the error message is simply implementing the definition from the .h file. (The note says “declared”, but that is actually an “implementation” - also kinda weird).
Any help would be much appreciated.