Pages: [1]   Go Down
Author Topic: [SOLVED] Inheritance doesn't work with main class functions in cpp file  (Read 379 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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:
#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:
#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:
#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:
#ifndef Analog_h
#define Analog_h

class Analog
{
public:
int getValue();

protected:
int _pin;
};

#endif

Analog.cpp
Code:
#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
« Last Edit: August 18, 2011, 04:45:00 am by Fredouille_91 » Logged

0
Offline Offline
Jr. Member
**
Karma: 1
Posts: 81
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 613
Posts: 49307
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Pages: [1]   Go Up
Jump to: