# Can't use base class and derived class functions

I have made two libraries and one test program.I have made my custom uart library.first i have included all functions for printing in single uart library file.but then i have made print library for printing functions only.my files are listed below.i have not posted my code directly so it can be read separately.

uart.h

uart.cpp

print.h

print.cpp

Test.cpp

#include <avr/io.h>
#include "uart.h"

int main(){
uart.start(9600);
uart.print("Test");
}


and i get this error..

Error

src/print.cpp
src/print.cpp: In member function 'void PrintClass::print(unsigned char)':
src/print.cpp:4:12: error: 'write' was not declared in this scope
write(data);
^
src/print.cpp: In member function 'void PrintClass::print(char*)':
src/print.cpp:13:22: error: 'write' was not declared in this scope
write(string[index]);
^
.build/uno/Makefile:47: recipe for target '.build/uno/src/print.o' failed make: *** [.build/uno/src/print.o] Error 1
Make failed with code 2


i have referred default arduino libraries like hardwareSerial and LiquidCrystal and i made my library like that but i cant get rid of that. Please Help me.

Any functions that the base class needs to call that will be defined in the child class must also be defined in the base class. They should be defined "virtual" so they can be overridden, and can even be "pure virtual" so they have to be defined in the child class.

So for any function in the child class that the base class needs to access, add this to the base class's definition (print.h):

virtual void write(unsigned char data) = 0;


The = 0 bit defines it as pure virtual, and any classes than inherit this class without defining their own copy of the void write(unsigned char) function will thrown an error.

Instead of pure virtual you can provide a default function that a child can override if they want to, which can be useful if most classes will work the same way (define it in the parent) but some want to do it differently (override it in the child), in which case you treat it like a normal class function but add the virtual tag.

The whole topic is called polymorphism and is an incredibly powerful system. It's discussed in detail here: http://www.cplusplus.com/doc/tutorial/polymorphism/

• if you dont mind,can you explain with my code..please – Mohit Tank Jun 15 '15 at 15:23
• All you should need is the one line of code I show there - add it to print.h – Majenko Jun 15 '15 at 15:24
• I dont know how can i thank you.thank you for helping again. :) . – Mohit Tank Jun 15 '15 at 17:15