Go Down

Topic: [SOLVED] Inheritance doesn't work with main class functions in cpp file (Read 441 times) previous topic - next topic

Aug 18, 2011, 10:14 am Last Edit: Aug 18, 2011, 11:45 am by Fredouille_91 Reason: 1
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 :
Code: [Select]

#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
Code: [Select]

#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
Code: [Select]

#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
Code: [Select]

#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
Code: [Select]

#ifndef Analog_h
#define Analog_h

class Analog
{
public:
int getValue();

protected:
int _pin;
};

#endif


Analog.cpp
Code: [Select]

#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

Nadir

Do you get any error while compiling? or it compiles right but not working properly.

PaulS

I copied and pasted all the code into separate tabs, so that all the files are in the same folder.

I had to change the include statement in the main sketch to use "" instead of <>.

I had to add #include "WProgram.h" to Analog.cpp.

Other than that, the program compiled just fine.


I copied and pasted all the code into separate tabs, so that all the files are in the same folder.
I had to change the include statement in the main sketch to use "" instead of <>.
I had to add #include "WProgram.h" to Analog.cpp.
Other than that, the program compiled just fine.


Thnak you very much, it's working now, my sketch is compiling without errors.

I just put all files in one folder (called sensors) and add "WProgram.h" to Analog.h.

Fred

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy