A.h
class A
{
private:
static const int b = 50;
int c[b];
};
A.cpp
#include "A.h"
const int A::b;
C.cpp
#include "A.h"
The compiler issues me a warning saying b is defined multiple times and one is ignored. I need to define it in the class since i need to initialize the array. Alternatively I would need to use the enum approach to do it. But I wanted to know if this was possible?
I'm going to guess that you are using Visual C++, which has a rather horrible language extension, as described in "Microsoft Extensions to C and C++":
The
/Ze
flag is enabled by default. You have to explicitly use the/Za
flag if you don't want the language extensions.The code as written compiles and links as-is without error using g++ 4.5.2, Clang 3.0, and Visual C++ 2010 with the
/Za
flag set.Removing the definition from the .cpp file resolves the issue if you want to compile with Visual C++, but then it won't work with other compilers (or with
/Za
) if you try to use the data member. For a portable workaround, you can use a conditional compilation block that checks to see whether the extensions are enabled: