multiple definition errors on compile

I have written code in a single file which works on an Arduino Mega2560 with display but when splitting it into .h and .cpp files I get multiple definition errors. I guess because I use functions within other functions in the same class. How do I get around this please?

Display.ino

#include <Display.h>

//		   "                "	
char testText1[] = "   Greg Cross   ";


void setup() {
	Display display(8,9,3);
	display.CLEAR();
	display.DisplayString(0,0,testText1,16);
}

void loop() {
  }

Display.h

#ifndef Display_h
#define Display_h

#include "Arduino.h"

class Display
{
	public:
		Display(int CS, int SID, int CLK);
		void WriteByte(int d);
		void WriteCommand(int cmd);
		void Initialise();
		void WriteData(int cmd);
		void CLEAR(void);
		void DisplayString(int X,int Y,char ptr[],int d);
	private:
		int _CS;
		int _SID;
		int _CLK;
		};
		
#endif

Display.cpp

#include <Display.h>


#include "Arduino.h"


Display::Display(int CS, int SID, int CLK)
{
	pinMode(CS, OUTPUT);
	pinMode(SID, OUTPUT);
	pinMode(CLK, OUTPUT);
	_CS = CS;
	_SID = SID;
	_CLK = CLK;
	}
	
void Display::WriteByte(int d)
{
    digitalWrite(_CS, HIGH);
    delay(5);
    shiftOut(_SID, _CLK, MSBFIRST, d);
    digitalWrite(_CS, LOW);
	}
	
void Display::WriteCommand(int cmd)
{
	int _H_data,_L_data;
	_H_data = cmd;
	_H_data &= 0xf0;           
	_L_data = cmd;             
	_L_data &= 0x0f;           
	_L_data <<= 4;            
	WriteByte(0xf8);         
	WriteByte(_H_data);
	WriteByte(_L_data);
   }
   
void Display::Initialise()
{
	digitalWrite(_CS, LOW);
	delay(80);
	WriteCommand(0x30);        
	WriteCommand(0x0c);        
	WriteCommand(0x01);       
	WriteCommand(0x06);
  }
  
void Display::WriteData(int cmd)
{
	int _H_data,_L_data;
	_H_data = cmd;
	_H_data &= 0xf0;           
	_L_data = cmd;            
	_L_data &= 0x0f;           
	_L_data <<= 4;             
	WriteByte(0xfa);          
	WriteByte(_H_data);
	WriteByte(_L_data);
  }
  
void Display::CLEAR()
{
    WriteCommand(0x30);
    WriteCommand(0x01);
	}
	
void Display::DisplayString(int X,int Y,char ptr[],int d)
{
	int i;
	  switch(X)
	   {
		 case 0:  Y|=0x80;break;

		 case 1:  Y|=0x90;break;

		 case 2:  Y|=0x88;break;

		 case 3:  Y|=0x98;break;

		 default: break;
	   }

  WriteCommand(Y); 

  for(i=0;i<d;i++)
    { 
      WriteData(ptr[i]);
    }
   }

Error List

Display\Display.cpp.o: In function `Display::WriteByte(int)':
C:\Program Files (x86)\Arduino\libraries\Display/Display.cpp:17: multiple definition of `Display::WriteByte(int)'
Display.cpp.o:C:\Users\Greg\AppData\Local\Temp\build6302322873889136374.tmp/Display.cpp:17: first defined here
c:/program files (x86)/arduino/hardware/tools/avr/bin/../lib/gcc/avr/4.3.2/../../../../avr/bin/ld.exe: Disabling relaxation: it will not work with multiple definitions
Display\Display.cpp.o: In function `Display::WriteData(int)':
C:\Program Files (x86)\Arduino\libraries\Display/Display.cpp:48: multiple definition of `Display::WriteData(int)'
Display.cpp.o:C:\Users\Greg\AppData\Local\Temp\build6302322873889136374.tmp/Display.cpp:48: first defined here
Display\Display.cpp.o: In function `Display::WriteCommand(int)':
C:\Program Files (x86)\Arduino\libraries\Display/Display.cpp:25: multiple definition of `Display::WriteCommand(int)'
Display.cpp.o:C:\Users\Greg\AppData\Local\Temp\build6302322873889136374.tmp/Display.cpp:25: first defined here
Display\Display.cpp.o: In function `Display::DisplayString(int, int, char*, int)':
C:\Program Files (x86)\Arduino\libraries\Display/Display.cpp:67: multiple definition of `Display::DisplayString(int, int, char*, int)'
Display.cpp.o:C:\Users\Greg\AppData\Local\Temp\build6302322873889136374.tmp/Display.cpp:67: first defined here
Display\Display.cpp.o: In function `Display::CLEAR()':
C:\Program Files (x86)\Arduino\libraries\Display/Display.cpp:61: multiple definition of `Display::CLEAR()'
Display.cpp.o:C:\Users\Greg\AppData\Local\Temp\build6302322873889136374.tmp/Display.cpp:61: first defined here
Display\Display.cpp.o: In function `Display::Initialise()':
C:\Program Files (x86)\Arduino\libraries\Display/Display.cpp:38: multiple definition of `Display::Initialise()'
Display.cpp.o:C:\Users\Greg\AppData\Local\Temp\build6302322873889136374.tmp/Display.cpp:38: first defined here
Display\Display.cpp.o: In function `Display':
C:\Program Files (x86)\Arduino\libraries\Display/Display.cpp:7: multiple definition of `Display::Display(int, int, int)'
Display.cpp.o:C:\Users\Greg\AppData\Local\Temp\build6302322873889136374.tmp/Display.cpp:7: first defined here
Display\Display.cpp.o: In function `Display':
C:\Program Files (x86)\Arduino\libraries\Display/Display.cpp:7: multiple definition of `Display::Display(int, int, int)'
Display.cpp.o:C:\Users\Greg\AppData\Local\Temp\build6302322873889136374.tmp/Display.cpp:7: first defined here
core.a(main.cpp.o): In function `main':
C:\Program Files (x86)\Arduino\hardware\arduino\cores\arduino/main.cpp:11: undefined reference to `setup'
C:\Program Files (x86)\Arduino\hardware\arduino\cores\arduino/main.cpp:14: undefined reference to `loop'

Why do you split it into .h and .cpp? If i am correct those are library extensions.. Are you trying to make a library or something ?

Do the files all "live" in the sketch folder? Or are you putting the display.cpp & display.h into the library folder? Or are you trying to have a new folder with your .h & .cpp files in there?

-jim lee

Do you have the display.cpp and display.h files in the library folder as well as in your project folder?
I’ve copied and pasted your code, placing display.cpp and display.h in the library folder, and display.ino in a project folder, and it compiles fine.

Thanks for your help TRex. I've put the Display.h and Display.cpp into a library called Display. Maybe I should try deleting the library and installing it again if it works on yours. What are your thoughts?

My first question was: “There is OK for this IDE that the sketch and some library have the same name?”
The second was: “Why did you do the include with ‘<>’ instead of with ’ “” '?”

Changing this 2 things it worked. Check the .zip file attached.

testDisplay.zip (1.53 KB)

I just added the library from the Sketch drop-down menu. I just changed my code to quotes and still made no difference. Should I just delete the library and load again?

gac9: I just added the library from the Sketch drop-down menu. I just changed my code to quotes and still made no difference. Should I just delete the library and load again?

Why you do that instead of download my .zip file, unzip it, open the testDisplay.ino, and compile it? You will see that it will compile (at least in my case, I compile it). And, BTW, delete the library that you add.

Luisilva That did it. Thanks so much. I had a copy of the header files in my project directory so they were getting done twice I guess. I remove them and the display works ! Regards Greg.

I said to remove the files in your libraries folder, but if it works it's OK too.

Good luck for the rest of the project.