Hello,
I have a problem with a inherited class wich can't access functions of the main class when they are defined in a cpp file. It works fine however when they are defined in the .h file as inline functions.
Then message I get when compiling is undefined reference to `MainClass::myFunction()'
Here is a sample code :
I defined a class called Analog containing default functions for all analog sensors (getValue, getVoltage,...).
I also defined a class IR inherited from Analog for specific IR sensor (adding GetDistance, for example)
First, my arduino sketch :
#include <IR.h>
IR irsensor = IR(2);
// Setup
void setup(){
Serial.begin(9600);
}
// Loop
void loop(){
Serial.println(irsensor.getValue());
Serial.println(irsensor.getDistance());
}
Then, my IR class code (inherited from Analog class) :
IR.h
#ifndef IR_h
#define IR_h
#include "WProgram.h"
#include "../Analog/Analog.h"
class IR : public Analog
{
public:
IR(int irPin);
float getDistance();
private:
};
#endif
IR.cpp
#include "IR.h"
IR::IR(int irPin){
_pin = irPin;
}
float IR::getDistance(){
int reading = analogRead(_pin);
return 12343.85 * pow(reading,-1.15);
}
And here is the code I'm using for the Analog class (this one work, using only a .h file) :
Analog.h
#ifndef Analog_h
#define Analog_h
class Analog
{
public:
inline int getValue(){
int reading = analogRead(_pin);
return reading;
}
protected:
int _pin;
};
#endif
Here is the code for the Analog class which is not working (using .cpp file):
Analog.h
#ifndef Analog_h
#define Analog_h
class Analog
{
public:
int getValue();
protected:
int _pin;
};
#endif
Analog.cpp
#include "Analog.h"
int Analog::getValue(){
int reading = analogRead(_pin);
return reading;
}
Do you have any idea of how I can use cpp file for my main classes ?
Thank you very much or your help,
Regards,
Fred