Loading...
Pages: [1]   Go Down
Author Topic: library hierarchy, inheritance  (Read 293 times)
0 Members and 1 Guest are viewing this topic.
Enschede - The Netherlands
Offline Offline
Jr. Member
**
Karma: 0
Posts: 66
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

I'd like to write a class that inherits from another class I've written. This works partly. Problem is that I cannot use functions in a function of the inherited class. I get this error:
Code:
/Users/macbook/Dropbox/code/_arduino/libraries/ColorLight/ColorLight_effect.cpp:27: error: 'lerpf' was not declared in this scope
/Users/macbook/Dropbox/code/_arduino/libraries/ColorLight/ColorLight_effect.cpp:32: error: 'colorInt' was not declared in this scope

I couldn't find any information on this. There is a tutorial, but thats partly finished:
http://www.arduino.cc/playground/Code/Hierarchy

This is the .h file of the "main" class
Code:
#ifndef ColorLight_h
#define ColorLight_h

#include "WProgram.h"

class ColourBase
{
  public:
    ColourBase(); 
   
    float interpolation;
   
  int baseHue;           
unsigned long wheelColor[4];
unsigned long tintColor[4];
unsigned long shadeColor[4];
unsigned long interpolatedColor[4];
};

class ColourPalette
{
  public:
    ColourPalette();
   
    void setHue(int);
void setComplementOffset(int);
void setHueAngle(int);

unsigned long getColour(byte);

unsigned long colorInt(unsigned long, unsigned long, unsigned long, unsigned long);
unsigned long colorInt(unsigned long, unsigned long, unsigned long);

  int mainHue;
int complementOffset;
int hueAngle;
int complementAngle;

int minSaturation;
int maxSaturation;
int minBrightness;
int maxBrightness;

ColourBase cArray[6];

void updatePalette();

void calculateShadeTint(ColourBase&);
void updateInterpolation(ColourBase&);
void calculateRGB(ColourBase &);

void getRGB(unsigned long*);

int   lerpf(int, int, float);
};

#endif

And the .h of the effect class that inherits from the ColourPalette.
Code:
#include "WProgram.h"

#ifndef ColorLight_effect_h
#define ColorLight_effect_h

#include "ColorLight.h"

class Effect : public ColourPalette
{ public:
Effect();

float time; // time
  float contrast;
 
  byte   noiseArray[23];
  int    colorArray[23];
  unsigned long colorMap[256];
 
  unsigned long lerpColorRGB(unsigned long, unsigned long, float);
};

#endif

In the lerpColorRGB function I'd like to use two functions of the ColourPalette class.
lerpf and colorInt

The .cpp file.
Code:
#include "WProgram.h"
#include "ColorLight_effect.h"

Effect::Effect()
{ for (int i=0;i<23; i++)
{ noiseArray[i] = 0;
colorArray[i] = 0;
};

for (int i=0;i<256; i++)
{ colorMap[i] = 0;
};
}

unsigned long lerpColorRGB(unsigned long c1, unsigned long c2, float amt)
{
  unsigned long a1 = (c1 >> 24) & 0xFF;
  unsigned long r1 = (c1 >> 16) & 0xff;
  unsigned long g1 = (c1 >> 8)  & 0xff;
  unsigned long b1 = c1 & 0xff;
 
  unsigned long a2 = (c2 >> 24) & 0xFF;
  unsigned long r2 = (c2 >> 16) & 0xff;
  unsigned long g2 = (c2 >> 8)  & 0xff;
  unsigned long b2 = c2 & 0xff;
 
  unsigned long ao = lerpf(a1, a2, amt);
  unsigned long ro = lerpf(r1, r2, amt);
  unsigned long go = lerpf(g1, g2, amt);
  unsigned long bo = lerpf(b1, b2, amt);
 
  return colorInt(ao,ro,go,bo);
}
Logged

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

The constructor for the Effect class does not actually create an instance of the class that it inherits from. So, you can not use the members of that class.

The Effect::Effect() method needs to create an instance of the ColourPalette class.

Code:
Effect::Effect() : ColourPalette()
{
  // Whatever the Effect constructor needs to do.
}
Logged

Enschede - The Netherlands
Offline Offline
Jr. Member
**
Karma: 0
Posts: 66
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I just tried to add this
Code:
Effect::Effect() : ColourPalette()
but it doesn't seem to make any difference I still get the errors. I noticed to when I remove this :ColourPalette() that the constructor of the colourpalette constructor is called the same.

Code:
started
constructor colourbase
constructor colourbase
constructor colourbase
constructor colourbase
constructor colourbase
constructor colourbase
constructor colourpalette
constructor effect

Can it be something else as well?
Logged

Enschede - The Netherlands
Offline Offline
Jr. Member
**
Karma: 0
Posts: 66
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I've found this introduction: http://www.cs.bu.edu/teaching/cpp/inheritance/intro/

Here I've found that I can say which method from which class I'd like to use:

I've modfied this in the .cpp file.
Code:
#include "WProgram.h"
#include "ColorLight_effect.h"

Effect::Effect() : ColourPalette()
{ /*
for (int i=0;i<23; i++)
{ noiseArray[i] = 0;
colorArray[i] = 0;
};

for (int i=0;i<256; i++)
{ colorMap[i] = 0;
};
*/
Serial.println("constructor effect");
}

unsigned long lerpColorRGB(unsigned long c1, unsigned long c2, float amt)
{
  unsigned long a1 = (c1 >> 24) & 0xFF;
  unsigned long r1 = (c1 >> 16) & 0xff;
  unsigned long g1 = (c1 >> 8)  & 0xff;
  unsigned long b1 = c1 & 0xff;
 
  unsigned long a2 = (c2 >> 24) & 0xFF;
  unsigned long r2 = (c2 >> 16) & 0xff;
  unsigned long g2 = (c2 >> 8)  & 0xff;
  unsigned long b2 = c2 & 0xff;
 
  unsigned long ao = ColourPalette::lerpf(a1, a2, amt);
  unsigned long ro = ColourPalette::lerpf(r1, r2, amt);
  unsigned long go = ColourPalette::lerpf(g1, g2, amt);
  unsigned long bo = ColourPalette::lerpf(b1, b2, amt);
 
  return ColourPalette::colorInt(ao,ro,go,bo);
}

I get now I slightly different error:
Code:
/Users/macbook/Dropbox/code/_arduino/libraries/ColorLight/ColorLight_effect.cpp: In function 'long unsigned int lerpColorRGB(long unsigned int, long unsigned int, float)':
/Users/macbook/Dropbox/code/_arduino/libraries/ColorLight/ColorLight_effect.cpp:30: error: cannot call member function 'int ColourPalette::lerpf(int, int, float)' without object
/Users/macbook/Dropbox/code/_arduino/libraries/ColorLight/ColorLight_effect.cpp:31: error: cannot call member function 'int ColourPalette::lerpf(int, int, float)' without object
/Users/macbook/Dropbox/code/_arduino/libraries/ColorLight/ColorLight_effect.cpp:32: error: cannot call member function 'int ColourPalette::lerpf(int, int, float)' without object
/Users/macbook/Dropbox/code/_arduino/libraries/ColorLight/ColorLight_effect.cpp:33: error: cannot call member function 'int ColourPalette::lerpf(int, int, float)' without object
/Users/macbook/Dropbox/code/_arduino/libraries/ColorLight/ColorLight_effect.cpp:35: error: cannot call member function 'long unsigned int ColourPalette::colorInt(long unsigned int, long unsigned int, long unsigned int, long unsigned int)' without object
Logged

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

Code:
unsigned long lerpColorRGB(unsigned long c1, unsigned long c2, float amt)
Isn't lerpColorRGB() supposed to be a class method?
Code:
unsigned long Effect::lerpColorRGB(unsigned long c1, unsigned long c2, float amt)
Logged

Enschede - The Netherlands
Offline Offline
Jr. Member
**
Karma: 0
Posts: 66
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you, that did it.
Logged

Pages: [1]   Go Up
Print
 
Jump to: