Trouble inheriting class from a library

I wanted to modify the coreBMP function in the Adafruit_ImageReader library. So I figured I'd create a new class that inherits from the Adafruit_ImageReader class. Then I can copy the coreBMP function rename it and tweak it for my purposes.

Initially, I thought the function I was looking for was in the Adafruit_Image class, so I started by inheriting that one.

I made an empty class and just tried changing my original code from:

Adafruit_Image img;

to my new class:

Adafruit_Image_Ext img;

The code compiled and executed fine. As a second test, I copied the draw function from Adafruit_Image, added it to my new class, called it drawMe and added some output to the serial monitor to verify it was using that function.
Updated my code from:

img.draw(tft, 0, 0);


img.drawMe(tft, 0, 0);

and again, all worked as expected.

So I deleted the function so I had an empty class again and changed it to inherit from Adafruit_ImageReader.

Then I updated

Adafruit_ImageReader reader(SD);


Adafruit_ImageReader_Ext reader(SD);

but it did not like that at all. I get this error message:

classTest:18:35: error: no matching function for call to 'Adafruit_ImageReader_Ext::Adafruit_ImageReader_Ext(SdFat&)'
 Adafruit_ImageReader_Ext reader(SD); // Image-reader object, pass in SD filesys

I'm not sure what the difference is. This is the working version of my class:

class Adafruit_Image_Ext : public Adafruit_Image {


And here's the version that fails to compile:

class Adafruit_ImageReader_Ext : public Adafruit_ImageReader {


I tried adding #include "Adafruit_Reader_Ext.h" and that gives me this error:

In file included from C:\Users\Tyler\Documents\Arduino\classTest\Adafruit_ImageReader_Ext.h:2:0,
                 from C:\Users\Tyler\Documents\Arduino\classTest\Adafruit_ImageReader_Ext.h:2,
                 from C:\Users\Tyler\Documents\Arduino\classTest\Adafruit_ImageReader_Ext.h:2,
                 from C:\Users\Tyler\Documents\Arduino\classTest\Adafruit_ImageReader_Ext.h:2,
                 from C:\Users\Tyler\Documents\Arduino\classTest\Adafruit_ImageReader_Ext.h:2,
                 from C:\Users\Tyler\Documents\Arduino\classTest\Adafruit_ImageReader_Ext.h:2,
                 from C:\Users\Tyler\Documents\Arduino\classTest\Adafruit_ImageReader_Ext.h:2,
                 from C:\Users\Tyler\Documents\Arduino\classTest\Adafruit_ImageReader_Ext.h:2,
                 from C:\Users\Tyler\Documents\Arduino\classTest\classTest.ino:15:
Adafruit_ImageReader_Ext.h:2:37: error: #include nested too deeply

I also tried various other includes from the library itself, but nothing seemed to work. Not sure why the one class from the library worked just fine but the other did not.

For reference, here's the library's .h file.

 * @file Adafruit_ImageReader.h
 * This is part of Adafruit's ImageReader library for Arduino, designed to
 * work with Adafruit_GFX plus a display device-specific library.
 * Adafruit invests time and resources providing this open source code,
 * please support Adafruit and open-source hardware by purchasing
 * products from Adafruit!
 * Written by Phil "PaintYourDragon" Burgess for Adafruit Industries.
 * BSD license, all text here must be included in any redistribution.

#include "Adafruit_SPIFlash.h"
#include "Adafruit_SPITFT.h"

/** Status codes returned by drawBMP() and loadBMP() */
enum ImageReturnCode {
  IMAGE_SUCCESS,            // Successful load (or image clipped off screen)
  IMAGE_ERR_FILE_NOT_FOUND, // Could not open file
  IMAGE_ERR_FORMAT,         // Not a supported image format
  IMAGE_ERR_MALLOC          // Could not allocate image (loadBMP() only)

/** Image formats returned by loadBMP() */
enum ImageFormat {
  IMAGE_NONE, // No image was loaded; IMAGE_ERR_* condition
  IMAGE_1,    // GFXcanvas1 image (NOT YET SUPPORTED)
  IMAGE_8,    // GFXcanvas8 image (NOT YET SUPPORTED)
  IMAGE_16    // GFXcanvas16 image (SUPPORTED)

   @brief  Data bundle returned with an image loaded to RAM. Used by
           ImageReader.loadBMP() and Image.draw(), not ImageReader.drawBMP().
class Adafruit_Image {
  int16_t width(void) const;  // Return image width in pixels
  int16_t height(void) const; // Return image height in pixels
  void draw(Adafruit_SPITFT &tft, int16_t x, int16_t y);
      @brief   Return canvas image format.
      @return  An ImageFormat type: IMAGE_1 for a GFXcanvas1, IMAGE_8 for
               a GFXcanvas8, IMAGE_16 for a GFXcanvas16, IMAGE_NONE if no
               canvas currently allocated.
  ImageFormat getFormat(void) const { return (ImageFormat)format; }
  void *getCanvas(void) const;
      @brief   Return pointer to color palette.
      @return  Pointer to an array of 16-bit color values, or NULL if no
               palette associated with image.
  uint16_t *getPalette(void) const { return palette; }
      @brief   Return pointer to 1bpp image mask canvas.
      @return  GFXcanvas1* pointer (1-bit RAM-resident image) if present,
               NULL otherwise.
  GFXcanvas1 *getMask(void) const { return mask; };

  union {                  // Single pointer, only one variant is used:
    GFXcanvas1 *canvas1;   ///< Canvas object if 1bpp format
    GFXcanvas8 *canvas8;   ///< Canvas object if 8bpp format
    GFXcanvas16 *canvas16; ///< Canvas object if 16bpp
  } canvas;                ///< Union of different GFXcanvas types
  GFXcanvas1 *mask;        ///< 1bpp image mask (or NULL)
  uint16_t *palette;       ///< Color palette for 8bpp image (or NULL)
  uint8_t format;          ///< Canvas bundle type in use
  void dealloc(void);      ///< Free/deinitialize variables
  friend class Adafruit_ImageReader; ///< Loading occurs here

   @brief  An optional adjunct to Adafruit_SPITFT that reads RGB BMP
           images (maybe others in the future) from a flash filesystem
           (SD card or SPI/QSPI flash). It's purposefully been made an
           entirely separate class (rather than part of SPITFT or GFX
           classes) so that Arduino code that uses GFX or SPITFT *without*
           image loading does not need to incur the RAM overhead and
           additional dependencies of the Adafruit_SPIFlash library by
           its mere inclusion. The syntaxes can therefore be a bit
           bizarre (passing display object as an argument), see examples
           for use.
class Adafruit_ImageReader {
  Adafruit_ImageReader(FatFileSystem &fs);
  ImageReturnCode drawBMP(const char *filename, Adafruit_SPITFT &tft, int16_t x,
                          int16_t y, boolean transact = true);
  ImageReturnCode loadBMP(const char *filename, Adafruit_Image &img);
  ImageReturnCode bmpDimensions(const char *filename, int32_t *w, int32_t *h);
  void printStatus(ImageReturnCode stat, Stream &stream = Serial);

  FatFileSystem *filesys; ///< FAT FileSystem Object
  File file;              ///< Current Open file
  ImageReturnCode coreBMP(const char *filename, Adafruit_SPITFT *tft,
                          uint16_t *dest, int16_t x, int16_t y,
                          Adafruit_Image *img, boolean transact);
  uint16_t readLE16(void);
  uint32_t readLE32(void);


Added Adafruit_ImageReader :: Adafruit_ImageReader; and it works now.

class Adafruit_ImageReader_Ext : public Adafruit_ImageReader {
        Adafruit_ImageReader :: Adafruit_ImageReader;


I think the problem was that you were using a constructor with an argument without having defined a constructor with an argument.

class Adafruit_ImageReader_Ext : 
public Adafruit_ImageReader 
  // Pass the class constructor argument to the base class constructor.
  Adafruit_ImageReader_Ext(FatFileSystem &fs) : Adafruit_ImageReader(fs) {}

Then you should be able to use:
Adafruit_ImageReader_Ext reader(SD);
without getting the error:
no matching function for call to 'Adafruit_ImageReader_Ext::Adafruit_ImageReader_Ext(SdFat&)'

Thank you John. I tried several variations of that and kept getting errors. I obviously wasn't structuring it properly. I added Adafruit_ImageReader_Ext(FatFileSystem &fs) : Adafruit_ImageReader(fs) {} and have no errors.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.