library hierarchy, inheritance

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:

/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

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

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

#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);
}

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.

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

I just tried to add this

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.

started
constructor colourbase
constructor colourbase
constructor colourbase
constructor colourbase
constructor colourbase
constructor colourbase
constructor colourpalette
constructor effect

Can it be something else as well?

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.

#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:

/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
unsigned long lerpColorRGB(unsigned long c1, unsigned long c2, float amt)

Isn't lerpColorRGB() supposed to be a class method?

unsigned long Effect::lerpColorRGB(unsigned long c1, unsigned long c2, float amt)

Thank you, that did it.