Error "...multiple definition of ..."

Hello,
I have spending hours trying to understand this probem... no way for me, I get errors when the linker get all stuffs together. Here is my basic example:

File zaza.ino

#include "MyClass.h"
MyClass totoClass;
void setup()
{
    totoClass.print();
}
void loop(){}

File MyClass.h

#ifndef MYCLASS
#define MYCLASS
#include <Dictionary.h>
class MyClass {
  public:
    MyClass();
    void print();
  private:
   Dictionary &errorTypesDico = *(new Dictionary(12)); 
   void initDicos();
};
#endif

File MyClass.cpp

#include "MyClass.h"
MyClass::MyClass() {}
void MyClass::print() {}
void MyClass::initDicos() {}

Compiling result:

Users/.../Documents/Arduino/libraries/Dictionary/src/Dictionary.h:580: multiple definition of `_ZN10Dictionary5jloadERK6Stringi'; /Users/bparis/zaza/build/sketch/myClass.cpp.o:/Users/bparis/Documents/Arduino/libraries/Dictionary/src/Dictionary.h:580: first defined here

collect2: error: ld returned 1 exit status

I understand that <Dictionary.h>
is called from program.cpp because of the #include "myClass.h"
then is called a second time when "myClass.h" is loaded from MyClass.cpp.
How to get rid of that ?
Thanks
Bernard

This is weird

Try

Dictionary errorTypesDico (12);

I've already tested this syntax in the past, it is refused:

image

By the way this is not related to my linker problem.

What’s the 12 supposed to mean?

Sometimes you get weird error messages.

What’s on line 580?

dictionary.h:580

It would help if you were either posting the full code or copy/paste (in code tags) the full compiler output

You shouldn't put a definitions of the functions and variables to the header file if you plan to include this header more than once. In that case you should to put you declarations in header file and definitions to .cpp file.

I don't understand what you mean because in my opinion the header file MyClass.h does contain only declarations.
A solution that is working is to put all (declarations + definitions) in the .h file and delete the .ccp related file, but is that THE right solution ?

I meant the Dictionary.h file, it seems that it contains definitions of variables.

If your project won't grow and you can be sure that won't need to separate Class.cpp and Class.h again in the future releases - this is solution.

i think the author of Dictionary.h put variables in the header

We would know more with an answer to

Hi,

well, I didn't thought that the problem could come from an inner code of a library, and here it is:
Dictionary.h

Thanks for you help and all responses, it's a bit clearer for me :+1:.

Bernard