ellysisland ellysisland - 2 months ago 6
C++ Question

C++ multiple definition linking error with const declared in .h

I am using some constants defined in

Constanst.h

#ifndef __CONSTANTS_H__
#define __CONSTANTS_H__

namespace A{
namespace B{

const int FIRST = 1;
const int SECOND = 2;
}
}
#endif


I am including this file from 2 different .cpp (A.cpp and B.cpp)

gcc version 3.2.3 is giving me "multiple definition" linking errors

I solve the problem only by including another "File.h" which includes "Constants.h" and declares the same constants as "extern"

Can someone explain why? I supposed this was an issue in C, not C++

Answer

With g++ version 4.3.4:

constants.h:5: error: ISO C++ forbids declaration of `FIRST' with no type

after adding type int, it works.

So maybe you should use a newer compiler? Use g++ instead of gcc?

Comments