I'm getting a redefinition error from my classes, but can't see what I did wrong.

Error: Icon.cpp:3:6: error: redefinition of 'void Icon::setIcon(byte (*)[8])'

Can any help me out on this? Thanks!

MyFile.ino

#include "MyIcon.h"

MyIcon myIcon;

MyIcon.h

#ifndef MyIcon_h
#define MyIcon_h

#include "Arduino.h"
#include "Icon.h"

class MyIcon : public Icon {
  public:
    MyIcon();
};

#endif

MyIcon.cpp

#include "MyIcon.h"

MyIcon::MyIcon() {

  byte _icon[8][8] = {
    {1, 1, 1, 1, 1, 1, 1, 1},
    {1, 1, 2, 2, 2, 2, 1, 1},
    {1, 2, 1, 1, 1, 1, 2, 1},
    {2, 1, 2, 2, 2, 2, 1, 2},
    {1, 2, 1, 1, 1, 1, 2, 1},
    {1, 1, 1, 2, 2, 1, 1, 1},
    {1, 1, 2, 1, 1, 2, 1, 1},
    {0, 0, 0, 1, 1, 0, 0, 0},
  };
  MyIcon::setIcon(_icon);

Icon.h

#ifndef Icon_h
#define Icon_h

#include "Arduino.h"

typedef struct{
  byte r;
  byte g;
  byte b;
} RGB;

class Icon {
  public:
    Icon();
    inline virtual void setIcon(byte icon[8][8]){};
    inline virtual void setIconColors(RGB colors[3]){};
};

#endif

Icon.cpp

#include "Icon.h"

void Icon::setIcon(byte icon[8][8]) {
  __icon = icon;
}


void Icon::setIconColors(RGB colors[3]) {
  __colors = colors;
}

Edited on 09/11/18

I attempted the following changes, before I realised thanks to KIIV, that I couldn't use array like that.

My idea was to be able to getIcon() and get back the byte array, which is later used to light up specific leds on a NeoMatrix panel.

I thought I would be able to build an icon "library" by extending a base class, setting the correct byte array in the class, and using it as needed (MyIcon, MyOtherIcon, etc...)

Have I completely missed the point?

Icon.h

void setIcon(byte icon[8][8]);
void setIconColors(RGB colors[3]);

Icon.cpp

#include "Icon.h"

Icon::Icon(){ }

void Icon::setIcon(byte icon[8][8]){
  memcpy(__icon, icon, sizeof(icon));
}


void Icon::setIconColors(RGB colors[3]){
  memcpy(__colors, colors, sizeof(colors));
}

closed as off-topic by Juraj, per1234, sempaiscuba, Greenonline, Majenko Nov 14 at 13:38

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question does not appear to be about Arduino, within the scope defined in the help center." – Juraj, per1234, sempaiscuba, Greenonline, Majenko
If this question can be reworded to fit the rules in the help center, please edit the question.

  • Hidden error: You might also what to check the MyIcon constructor. What type of variable (_icon) are you passing to "Icon::setIcon()"? – Mikael Patel Nov 8 at 23:13
  • 1
    this is an example of a question for Stack Overflow, not for Arduino SE. arduino.stackexchange.com/tour – Juraj Nov 9 at 6:40
  • You should make the Icon pure virtual by virtual void setIcon(byte icon[8][8]) = 0; and same for setIconColors. In Icon.cpp is total nonsense. Even if you define __icon and _colors in MyIcon as members, Icon don't know anything about it. And arrays cannot be assigned like this. – KIIV Nov 9 at 7:29
  • I think I got way lost in my attempts at achieving what I wanted to do. I wanted to be able to extend a base class (Icon) in order to be able to build an icon "library" of sorts.The icons are built from an 8x8 byte array, so I wanted to be able to request getIcon() from a child class like MyIcon in order to retrieve the icon (which is then use to light up a NeoMatrix panel. I didn't realize you couldn't return (or set) a byte array from a function. – Soch S. Nov 9 at 10:33
  • I would suggest you don't want to use classes at all for this. You can have an icon as a single array of data in PROGMEM and write a little function to parse it. I'd have something like one byte for the number of colours (N), two bytes for the icon dimensions (W,H) (if needed), N*3 bytes for the colour information, then WxH bytes for the icon image. If the number of colours is always the same, or the dimensions of the icon is always the same then you could omit N and/or W,H values. – Majenko Nov 9 at 12:40

You have bodies for setIcon and setIconColors in both the header file and the cpp file. You can only have them in one or the other - choose.

Also, as an aside, the way that you are assigning icon data will go out of scope before you use it. You create a local array variable which you then pass a pointer to to your constructor. That pointer is then saved. But then you go out of scope and the pointer is discarded, and the data will be overwritten.

As Majenko writes, you are having two bodies.

The bodies in the header file are empty and probably you do not want this. It is normal to implement the functions in the cpp file (which you do), so in the header file remove the {} in

inline virtual void setIcon(byte icon[8][8]){};
inline virtual void setIconColors(RGB colors[3]){};

so it becomes

inline virtual void setIcon(byte icon[8][8]);
inline virtual void setIconColors(RGB colors[3]);

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