2

I have the following errors trying to use a TFT library function inside my library. My library uses the pointer to an instance of the TFT library in the constructor, as well described there:

Basic C++ programming, how to pass constructor argument into class?

The errors are:

error: invalid use of incomplete type 'class TFT'
_TFTscreen->background(0, 0, 0);

error: forward declaration of 'class TFT'
class TFT;

MESmenu.h:

#ifndef _MESMENU_H_
#define _MESMENU_H_

#include <SPI.h>
#include <SD.h>

class TFT;

class MESmenu{
public:
  MESmenu(TFT* pTFT) : _TFTscreen(pTFT) {};
  void start();
  void execute();
private:
  TFT* _TFTscreen;
};

#endif

MESmenu.cpp:

#include "Arduino.h"
#include "MESmenu.h"

void MESmenu::start(){
  _TFTscreen->background(0, 0, 0);
  _TFTscreen->text("starting menu...", 0, 0);
  //...
}

void MESmenu::execute(){
   //some code...  
}

The Arduino sketch is:

#include <SPI.h>
#include <SD.h>
#include <TFT.h>  // Arduino LCD library
#include <MESmenu.h>  // my library

// --------- TFT LCD ---------
// pin definition for the due
#define sd_cs  7
#define lcd_cs 10
#define dc     9
#define rst    8
TFT TFTscreen = TFT(lcd_cs, dc, rst);

// MES menu class
MESmenu *mainMenu = new MESmenu(&TFTscreen);

void setup() {
  // initialize TFT
  TFTscreen.begin();
  TFTscreen.background(0, 0, 0);
  // set the stroke color to white
  TFTscreen.stroke(255,255,255);
  TFTscreen.fill(255,255,255);
  TFTscreen.setTextSize(2);
  TFTscreen.text("TEST", 0, 0);

  // Start menu
  mainMenu->start();
}

void loop () {
  mainMenu->execute();      
}

The problem is related to the function:

_TFTscreen->background(0, 0, 0);

inside the MESmenu::start() in the .cpp file.

UPDATE:

As suggested by frarugi87 I've updated the question with the details of .h and .cpp files. Those file are in the Arduino\libraries\MESmenu folder.

I try to change

class TFT;

with

#include <TFT.h>

and removed the #include from the main arduino sketch.

This leads to the following errors:

libraries\MESmenu\MESmenu.cpp.o: In function `Adafruit_GFX::image(PImage&, unsigned int, unsigned int)':
D:\Software\arduino-1.6.7\libraries\TFT\src/utility/Adafruit_GFX.h:231: multiple definition of `Adafruit_GFX::image(PImage&, unsigned int, unsigned int)'
sketch\test_MESmenu_lib.ino.cpp.o:D:\Software\arduino-1.6.7\libraries\TFT\src/utility/Adafruit_GFX.h:231: first defined here

libraries\MESmenu\MESmenu.cpp.o: In function `Adafruit_GFX::image(PImage&, unsigned int, unsigned int)':
D:\Software\arduino-1.6.7\libraries\TFT\src/utility/Adafruit_GFX.h:231: multiple definition of `PImage::read16(SDLib::File)'
sketch\test_MESmenu_lib.ino.cpp.o:D:\Software\arduino-1.6.7\libraries\TFT\src/utility/PImage.h:17: first defined here

libraries\MESmenu\MESmenu.cpp.o: In function `Adafruit_GFX::image(PImage&, unsigned int, unsigned int)':
D:\Software\arduino-1.6.7\libraries\TFT\src/utility/Adafruit_GFX.h:231: multiple definition of `PImage::read32(SDLib::File)'
sketch\test_MESmenu_lib.ino.cpp.o:D:\Software\arduino-1.6.7\libraries\TFT\src/utility/PImage.h:17: first defined here

libraries\MESmenu\MESmenu.cpp.o: In function `Adafruit_GFX::image(PImage&, unsigned int, unsigned int)':
D:\Software\arduino-1.6.7\libraries\TFT\src/utility/Adafruit_GFX.h:231: multiple definition of `PImage::loadImage(char const*)'
sketch\test_MESmenu_lib.ino.cpp.o:D:\Software\arduino-1.6.7\libraries\TFT\src/utility/PImage.h:17: first defined here
  • Please add the actual source code that gives the problem. It looks like you have forgot to include the TFT header file in the menu source file. – Mikael Patel Mar 29 '16 at 8:05
  • The posted code is almoste complete. I have the MESmenu.cpp file that contais: #include "Arduino.h" #include "MESmenu.h" void MESmenu::execute(){ _TFTscreen->background(0, 0, 0); } And this give me the error. If I put the code of the function "execute" in the .h file it works. If the code is in the .cpp file I got the error. I cannot include the TFT.h library in the .h or .ccp files, becouse it leads to "multiple definition" – Davide Tagliafierro Mar 29 '16 at 8:59
1

Try to change

class TFT;

to

#include <TFT.h>

Then you can remove the #include <TFT.h> from the main arduino sketch.

Moreover you should at least write a piece of code to make the compiler include your library just once; this is the usual way to do it:

#ifndef _MESMENU_H_
#define _MESMENU_H_

#include <SPI.h>

...
the rest of your library
...

  TFT* _TFTscreen;
}

#endif

_MESMENU_H_ is any unique string (usually the filename is used, remembering that you can't put spaces or other punctuation marks except the underscore). And you should put it only in the header file (*.h or .hpp), not in the source (.c or *.cpp or *.ino)

EDIT:

The additional info lead me to analyze the library and.. there are problems with the library. Particularly the file TFT/src/utility/Adafruit_GFX.h has a big problem: it has the code for the functions.

To fix it, you have to modify the library.

Go into the folder TFT/src/utility/ and make a copy of Adafruit_GFX.h, calling it PImage.cpp.

Open Adafruit_GFX.h and delete everything between line 227 and line 370. This means that from

bool useFill;
};

#if defined(__SD_H__)  // Arduino SD library

#define BUFFPIXEL 20

void Adafruit_GFX::image(PImage & img, uint16_t x, uint16_t y) {

...

  return PImage(bmpFile, bmpWidth, bmpHeight, bmpDepth, bmpImageoffset, rowSize, flip);
}

#endif

#endif // _ADAFRUIT_GFX_H

it should become

bool useFill;
};

#endif // _ADAFRUIT_GFX_H

Having done this, open PImage.cpp and delete everything above line 227, then delete the function Adafruit_GFX::image and the last endif and add, at the beginning, #include Adafruit_GFX.h. This means that the file should look like

#include "Adafruit_GFX.h"

#if defined(__SD_H__)  // Arduino SD library

#define BUFFPIXEL 20

// These read 16- and 32-bit types from the SD card file.
// BMP data is stored little-endian, Arduino is little-endian too.
// May need to reverse subscript order if porting elsewhere.

uint16_t PImage::read16(File f) {

...  

  return PImage(bmpFile, bmpWidth, bmpHeight, bmpDepth, bmpImageoffset, rowSize, flip);
}

#endif

Then try to compile again the program

BTW: the source I looked at is at this page.

  • I try with your suggestion, but now I got "multiple definition" releated to Adafruit_GFX.h that is included in the TFT library. Do you think that the problem could be in the TFT library itself? – Davide Tagliafierro Mar 29 '16 at 9:09
  • @DavideTagliafierro multiple definitions of what? – frarugi87 Mar 29 '16 at 9:19
  • libraries\MESmenu\MESmenu.cpp.o: In function 'Adafruit_GFX::image(PImage&, unsigned int, unsigned int)': D:\Software\arduino-1.6.7\libraries\TFT\src/utility/Adafruit_GFX.h:231: multiple definition of 'Adafruit_GFX::image(PImage&, unsigned int, unsigned int)' sketch\test_MESmenu.ino.cpp.o:D:\Software\arduino-1.6.7\libraries\TFT\src/utility/Adafruit_GFX.h:231: first defined here – Davide Tagliafierro Mar 29 '16 at 9:40
  • Ok, and where is MESmenu.cpp? Can you post the whole modified files in the question? Or at least the ino, mesmenu.h and mesmenu.cpp – frarugi87 Mar 29 '16 at 11:15
  • Question updated with more details. I try to move all the functions from the .cpp file to the .h file it works (not tested on the board, but it compiles). I still doesn't understand why. – Davide Tagliafierro Mar 29 '16 at 11:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.