serial lcd library error

Hi,

I have been trying to create a library based on the code from here: http://www.arduino.cc/playground/Learning/SparkFunSerLCD

And ran into the problem where I keep getting the following error: /home/xxxxx/arduino-0021/libraries/Seriallcd/Seriallcd.cpp:7: Error: implicit declaration >>Seriallcd::Seriallcd()<<

Here is my code for Seriallcd.cpp

/*Written by Frederick Hunter 17.12.2010 based on the Arduino.cc
website http://www.arduino.cc/playground/Learning/SparkFunSerLCD
*/
#include "WProgram.h"
#include "Seriallcd.h"

Seriallcd::Seriallcd(){
  Serial.begin(9600);
}

void Seriallcd::selectLineOne(){  
   Serial.print(0xFE, BYTE);  
   Serial.print(128, BYTE);    
}
void Seriallcd::selectLineTwo(){  
   Serial.print(0xFE, BYTE);   
   Serial.print(192, BYTE);   
}
void Seriallcd::goTo(int position) { 
if (position<16){
  Serial.print(0xFE, BYTE);   
  Serial.print((position+128), BYTE);   
}
else if (position<32){
  Serial.print(0xFE, BYTE);   
  Serial.print((position+48+128), BYTE);   
} 
else {
  goTo(0);
}
}

void Seriallcd::clearLCD(){
   Serial.print(0xFE, BYTE);   
   Serial.print(0x01, BYTE); 
}
void Seriallcd::backlightOn(){ 
    Serial.print(0x7C, BYTE);   
    Serial.print(157, BYTE);   
}
void Seriallcd::backlightOff(){  
    Serial.print(0x7C, BYTE);   
    Serial.print(128, BYTE);     
}
void Seriallcd::serCommand(){   
  Serial.print(0xFE, BYTE);
}

Here is my code for Seriallcd.h

/*Written by Frederick Hunter 17.12.2010 based on the Arduino.cc
website http://www.arduino.cc/playground/Learning/SparkFunSerLCD
*/
/*PRE & POST Conditions:
 * void selectLineOne();
 * PRE: NONE
 * POST: moves cursor to the first position in the top line
 * 
 * void selectLineTwo();
 * PRE: NONE
 * POST: moves cursor to the first position on the bottom line
 * 
 * void goTo(int position);
 * PRE: position is the block number you want to put the cursor to (0-15 is line 1 & 16-31 is line two, 31+ sets it back to 0)
 * POST: moves cursor to defined block position
 * 
 * void clearLCD();
 * PRE: NONE
 * POST: erases everything on the LCD screen
 * 
 * void backlighOn();
 * PRE: NONE
 * POST: Turns on the backlight
 * 
 * void backlightOff();
 * PRE: NONE
 * POST: Turns the backlight off
 * 
 * void serCommand();
 * PRE: NONE
 * POST: calls specified command
 */
 


#ifndef Seriallcd_h
#define Seriallcd_h

#include "WProgram.h"

class Seriallcd{
  public:
    void selectLineOne();
    void selectLineTwo();
    void goTo(int position);
    void clearLCD();
    void backlightOn();
    void backlightOff();
    void serCommand();
};

#endif

And finally for my main program:

#include <Seriallcd.h>

/*Sample code for the SerialLcd library which shows the time in millis on the top and bottom lines of the LCD.
*/
Seriallcd lcd();


void setup()
{
}

void loop()
{  
  Seriallcd.selectLineOne();
  delay(100);
  Serial.print(millis());
  Seriallcd.selectLineTwo();
  delay(100);
  Serial.print(millis()/2);
  delay(100);
}

I based my library from the example on this site:
http://www.arduino.cc/en/Hacking/LibraryTutorial

I think the problem lies where I am declaring the class Seriallcd in Seriallcd.cpp

Seriallcd::Seriallcd(){
Serial.begin(9600);
}

When i change it to
Seriallcd::lcd(){
Serial.begin(9600);
}

I get another error saying something like it needs a type declaration.

Any ideas what I am doing wrong?

Thanks in advance.

In your class definition, in the .h file, you have not declared that the class HAS a constructor. In the implementation file, the .cpp file, you DO define a constructor. The compiler is telling you that you need to fix one or the other.

The Serial.begin() call should NOT be in your constructor. You have no way of knowing whether your constructor will be called before the Serial instance is constructed, or after. If your is called first, you will never get any serial output.

You'll notice that the Serial class has a begin() method that is invoked in setup() or loop(). This is so that the important information that the Serial instance needs can be passed to it AFTER the instance has been created.

You should add a begin() method to your class, and invoke Serial.begin() there.

Actually, you should not invoke Serial.begin() at all. The users of you class may have different speed requirements than you have. If you hard code a speed, or that fact that Serial IS initialized, you limit the usefulness of the class. Of course, this is general advice, and less useful here when Serial MUST be initialized somewhere in order for your class to be useful.

You should think about making your class more flexible, so that it can work with the Mega which has more than one hardware serial port, and with NewSoftSerial when the hardware serial port is being used for other purposes.

Thank you Paul, I ended up looking at this website and followed their instructions.
http://www.arduino.cc/playground/Code/Library

I did get it to work in the end and here is the code if anyone is interested. You are right, I should add the suggested functions, but for my purposes, I believe this will do for now.

.cpp code:

/*Written by Frederick Hunter 17.12.2010 based on the Arduino.cc
website http://www.arduino.cc/playground/Learning/SparkFunSerLCD
*/
#include "WProgram.h"
#include "Seriallcd.h"

Seriallcd::Seriallcd(){
}
Seriallcd::~Seriallcd(){
}

void Seriallcd::selectLineOne(){  
   Serial.print(0xFE, BYTE);  
   Serial.print(128, BYTE);    
}
void Seriallcd::selectLineTwo(){  
   Serial.print(0xFE, BYTE);   
   Serial.print(192, BYTE);   
}
void Seriallcd::goTo(int position) { 
if (position<16){
  Serial.print(0xFE, BYTE);   
  Serial.print((position+128), BYTE);   
}
else if (position<32){
  Serial.print(0xFE, BYTE);   
  Serial.print((position+48+128), BYTE);   
} 
else {
  goTo(0);
}
}

void Seriallcd::clearLCD(){
   Serial.print(0xFE, BYTE);   
   Serial.print(0x01, BYTE); 
}
void Seriallcd::backlightOn(){ 
    Serial.print(0x7C, BYTE);   
    Serial.print(157, BYTE);   
}
void Seriallcd::backlightOff(){  
    Serial.print(0x7C, BYTE);   
    Serial.print(128, BYTE);     
}
void Seriallcd::serCommand(){   
  Serial.print(0xFE, BYTE);
}

.h code:

/*Written by Frederick Hunter 17.12.2010 based on the Arduino.cc
website http://www.arduino.cc/playground/Learning/SparkFunSerLCD
*/
/*PRE & POST Conditions:
 * void selectLineOne();
 * PRE: NONE
 * POST: moves cursor to the first position in the top line
 * 
 * void selectLineTwo();
 * PRE: NONE
 * POST: moves cursor to the first position on the bottom line
 * 
 * void goTo(int position);
 * PRE: position is the block number you want to put the cursor to (0-15 is line 1 & 16-31 is line two, 31+ sets it back to 0)
 * POST: moves cursor to defined block position
 * 
 * void clearLCD();
 * PRE: NONE
 * POST: erases everything on the LCD screen
 * 
 * void backlighOn();
 * PRE: NONE
 * POST: Turns on the backlight
 * 
 * void backlightOff();
 * PRE: NONE
 * POST: Turns the backlight off
 * 
 * void serCommand();
 * PRE: NONE
 * POST: calls specified command
 */
 


#ifndef SERIALLCD_H
#define SERIALLCD_H

#include <WProgram.h>

class Seriallcd{
  public:
    Seriallcd();
    ~Seriallcd();
    void selectLineOne();
    void selectLineTwo();
    void goTo(int position);
    void clearLCD();
    void backlightOn();
    void backlightOff();
    void serCommand();
};

#endif

main program:

/*Sample code for the SerialLcd library which shows the time in millis on the top and bottom lines of the LCD.
*/
#include <Seriallcd.h>
Seriallcd lcd;


void setup()
{
  Serial.begin(9600);
  lcd.backlightOn();
}

void loop()
{  
  lcd.selectLineOne();
  delay(100);
  Serial.print(millis());
  lcd.selectLineTwo();
  delay(100);
  Serial.print(millis()/2);
  delay(100);
}