Problem with inheriting between Stream base and derived classes

05 Oct 2010

Hi,

I'm trying to write a class for a 16x2 serial LCD display. One of the functions I'm trying to inherit is the printf function from the Stream class, similar to the TextLCD or Textdisplay libs from Simon Ford.

For some reason, I cannot get the code to compile and get the following compiler output:

"Object of abstract class type "LCD16x2" is not allowed: (E322)" in file "/MyLCD/main.cpp"
"LCD16x2 LCD (p28); (E0)" in file "/MyLCD/main.cpp"
"^ (E0)" in file "/MyLCD/main.cpp"

I've already stripped down the code to the bare minimum but cannot pinpoint what the problem is. Can someone help me? Thanks in advance.

Here's my main.cpp

#include "mbed.h"
#include "LCD16x2.h"
LCD16x2 LCD (p28);
int main() {
    int value=100;
    while (1) {
        LCD.printf("Testing : %i",value);
    }
}
Here's the LCD16x2.h

#ifndef LCD16x2_H
#define LCD16x2_H
#include "mbed.h"

class LCD16x2:public Stream {
public:
    LCD16x2(PinName rx);
    int DoSomething(int number);
protected:
    Serial _serial;  
};
#endif
And the LCD16x2.cpp

#include "LCD16x2.h"

LCD16x2::LCD16x2(PinName rx):_serial(rx,NC) {
}

int LCD16x2::DoSomething(int number) {
    number++;
    return number;
}

05 Oct 2010 . Edited: 05 Oct 2010

Hi,

If your display does just use a single pin serial UART then you need to inherit from Serial not Stream.

Try this

for LCD16x2.h

 

#ifndef LCD16x2_H
#define LCD16x2_H
#include "mbed.h"

class LCD16x2 : public Serial {
public:
    LCD16x2(PinName rx);
    int DoSomething(int number);
protected:
    
};
#endif

and for LCD16x2.cpp

 

#include "LCD16x2.h"

LCD16x2::LCD16x2(PinName rx): Serial(rx,NC) {
}

int LCD16x2::DoSomething(int number) {
    number++;
    return number;
}

Should work but i haven't checked it in the compiler myself.

 

Matt

05 Oct 2010

Hi Matt,

Thanks a lot! That did the trick :-)

Kr, Wim

06 Oct 2010

You might want to have a look at this:

http://mbed.org/users/giryan/libraries/TextLCD_Serial/le8d7w

I've not really "finished" it, but feel free to grab it :)