Inheriting from a lower level library.

Hi,

I've written a couple of my own libraries without too many problems, but I've moved onto a new project involving LCD displays and am having problems.

What I want to do is produce a library that contains all the existing functions of LiquidCrystal.h plus a few of my own, for the moment I'd just like to add a couple of extra functions, one to prefix any number with the appropriate positive or negative sign, and one to right-justify a number.

Here's what I've come-up with so far.
LiquidCrystalFuncs.h......

#ifndef LiquidCrystalFuncs_h                        
#define LiquidCrystalFuncs_h                        

extern "C" {
  #include <string.h>
}

#include <stdlib.h>                        
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"                       
#else
#include "WProgram.h"                      
#endif
#include "LiquidCrystalFuncs.h"            // header for this library
#include <LiquidCrystal.h>                 // Library for controlling a LCD display.

class LiquidCrystalFuncs  
{
  public:
    //LiquidCrystalFuncs();                   // Constructor
    LiquidCrystalFuncs(uint8_t rs, 
                       uint8_t enable,
		                   uint8_t d0, 
                       uint8_t d1, 
                       uint8_t d2,
                       uint8_t d3);
    void begin(void);                       // Setup object
    void setCursor(uint8_t, uint8_t); 
    void print(int x);
    void printS(int x);                     // Print a number with an appropriate sign.                       
    void printR(int x);                     // Print a right-justified number.                       

  private:
    // Member variables
    LiquidCrystal _lcd;                     // lcd
    // Private functions                   
};
#endif

LiquidCrystalFuncs.cpp.....

extern "C" {
  #include <string.h>
}

#include <stdlib.h>                        
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"                       
#else
#include "WProgram.h"                     
#endif
#include "LiquidCrystalFuncs.h"            // header for this library
#include <LiquidCrystal.h>                 // Library for controling a LCD display.

/**************************************************************************
  Constructor
**************************************************************************/
LiquidCrystalFuncs::LiquidCrystalFuncs(uint8_t rs, uint8_t enable, uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3)
{
  _lcd.LiquidCrystal(rs, enable, d0, d1, d2, d3);
}


/**************************************************************************
  Do stuff
**************************************************************************/
void LiquidCrystalFuncs::begin(void)                       
{
  _lcd.begin(20, 2);
}


/**************************************************************************
  Print
**************************************************************************/
void LiquidCrystalFuncs::print(int x)                       
{
  _lcd.print(x);
}


/**************************************************************************
  Print (with a +ve or -ve sign)
**************************************************************************/
void LiquidCrystalFuncs::printS(int x)                       
{
  if (x < 0)
  {
    _lcd.print("-");
    _lcd.print(x);
  }
  else
  {
    _lcd.print("+");
    _lcd.print(x);
  }
}

/**************************************************************************
  Print a right justified integer. For the moment, assume that it's going
  to be a maximum of fours characters.
**************************************************************************/
void LiquidCrystalFuncs::printR(int x)                       
{
  if (x < 9)
  {
    _lcd.print("   "); // Three spaces
    _lcd.print(x);
  }
  else if (x < 99)
  {
    _lcd.print("  "); // Two spaces
  }
  else if (x < 999)
  {
    _lcd.print(" "); // One space
  }
  else
  {
    Serial.println("ooops!");
  }
  _lcd.print(x);
}

I'm pretty sure it's not rocket science I'm trying to do, but getting syntax right is beyond me. Here are the current errors....

C:\all_apps\arduino-1.0\libraries\LiquidCrystalFuncs\LiquidCrystalFuncs.cpp: In constructor 'LiquidCrystalFuncs::LiquidCrystalFuncs(uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t)':
C:\all_apps\arduino-1.0\libraries\LiquidCrystalFuncs\LiquidCrystalFuncs.cpp:29: error: no matching function for call to 'LiquidCrystal::LiquidCrystal()'
C:\all_apps\arduino-1.0\libraries\LiquidCrystal/LiquidCrystal.h:56: note: candidates are: LiquidCrystal::LiquidCrystal(uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t)
C:\all_apps\arduino-1.0\libraries\LiquidCrystal/LiquidCrystal.h:54: note: LiquidCrystal::LiquidCrystal(uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t)
C:\all_apps\arduino-1.0\libraries\LiquidCrystal/LiquidCrystal.h:52: note: LiquidCrystal::LiquidCrystal(uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t)
C:\all_apps\arduino-1.0\libraries\LiquidCrystal/LiquidCrystal.h:49: note: LiquidCrystal::LiquidCrystal(uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t)
C:\all_apps\arduino-1.0\libraries\LiquidCrystal/LiquidCrystal.h:45: note: LiquidCrystal::LiquidCrystal(const LiquidCrystal&)
C:\all_apps\arduino-1.0\libraries\LiquidCrystalFuncs\LiquidCrystalFuncs.cpp:33: error: invalid use of 'class LiquidCrystal'

I suspect this is one of those 'it's simple when you know how problems'

Cheers

Your post title says "Inherit" while the C++ term is derive. You are doing neither.

You need to construct the LiquidCrystal instance at the same time as you construct your class:

LiquidCrystalFuncs::LiquidCrystalFuncs(uint8_t rs, uint8_t enable, uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3) :   _lcd(rs, enable, d0, d1, d2, d3);
{
}

while the C++ term is derive

Many of the available tutorials use the phrase 'inheritance' - but who am I to argue!

You are doing neither.

yeah - I figured that from the number of errors I was getting :smiley:

Based on the above post (thank you PaulS) plus this tutorial...

and this one.....

I've now got some working code....
LiquidCrystalFuncs.h......

#ifndef LiquidCrystalFuncs_h                        
#define LiquidCrystalFuncs_h                        

extern "C" {
  #include <string.h>
}

#include <stdlib.h>                        // needed for 'free'
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"                       // replacement for "WProgram.h" - required for versions after to 1.0.1
#else
#include "WProgram.h"                      
#endif
#include "LiquidCrystalFuncs.h"            // header for this library
#include <avr/pgmspace.h>                  
#include <ProgmemConst.h>                  
#include <LiquidCrystal.h>                 // Library for controling a LCD display.

// class derived from LiquidCrystal
class LiquidCrystalFuncs : public LiquidCrystal
{
  public:
    LiquidCrystalFuncs(uint8_t rs, uint8_t enable, uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3) : LiquidCrystal(rs, enable, d0, d1, d2, d3){};

    void printS(int x);                     // Print a number with an appropriate sign.                       
    void printR(int x);                     // Print a right-justified number.                       

  private:
    // Member variables
    // Private functions
                     
};

#endif

LiquidCrystalFuncs.cpp......

extern "C" {
  #include <string.h>
}

#include <stdlib.h>                        
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"                       
#else
#include "WProgram.h"                      
#endif
#include "LiquidCrystalFuncs.h"            
#include <avr/pgmspace.h>                  
#include <ProgmemConst.h>
#include <LiquidCrystal.h>                 // Library for controling a LCD display.

/**************************************************************************
  Print (with a +ve or -ve sign)
**************************************************************************/
void LiquidCrystalFuncs::printS(int x)                       
{
  if (x < 0)
  {
    print("-");
    print(x);
  }
  else
  {
    print("+");
    print(x);
  }
}

/**************************************************************************
  Print a right justified integer. For the moment, assume that it's going
  to be a maximum of six characters.
**************************************************************************/
void LiquidCrystalFuncs::printR(int x)                       
{
  if (x < 9)
  {
    print("     "); // five spaces
    print(x);
  }
  else if (x < 99)
  {
    print("    "); // four spaces
  }
  else if (x < 999)
  {
    print("   "); // three space
  }
  else if (x < 9999)
  {
    print("  "); // two space
  }
  else if (x < 99999)
  {
    print(" "); // one space
  }
  else
  {
    Serial.println("ooops!");
  }
  print(x);
}

Thanks for your help.