Linkage problems with in-class static members

N

Nico

I got a weired linkage problem, trying to initialize a member of a
class with a static const.
The problem could be condensed into the following little program:
//file test.cpp:
class T
{
public:
static const int STATIC_MEMBER_A = 1;
static const int STATIC_MEMBER_B = 2;
T(int x);
private:
int t;
};

T::T(int x): t(x > 0? STATIC_MEMBER_A: STATIC_MEMBER_B) {}

int main(int argnum, char** args)
{
T t(-5);
}

//end file test.cpp

I'm getting following reproducable error in linkage:
/tmp/cc0UEe2g.o: In function `T::T(int)':
test.cpp:(.text+0x13): undefined reference to `T::STATIC_MEMBER_A'
test.cpp:(.text+0x1e): undefined reference to `T::STATIC_MEMBER_B'
/tmp/cc0UEe2g.o: In function `T::T(int)':
test.cpp:(.text+0x43): undefined reference to `T::STATIC_MEMBER_A'
test.cpp:(.text+0x4e): undefined reference to `T::STATIC_MEMBER_B'
collect2: ld returned 1 exit status

I'm using g++ (GCC) 4.1.2 20061115 (prerelease) (Debian 4.1.1-21).

If I change
T::T(int x): t(x > 0? STATIC_MEMBER_A: STATIC_MEMBER_B) {}
to
T::T(int x): t(x > 0? STATIC_MEMBER_A: 9) {}
the program can be linked and executed without any error.

Is this a bug in g++? Does anybody have a clue?
 
V

Victor Bazarov

Nico said:
I got a weired linkage problem, trying to initialize a member of a
class with a static const.
The problem could be condensed into the following little program:
//file test.cpp:
class T
{
public:
static const int STATIC_MEMBER_A = 1;
static const int STATIC_MEMBER_B = 2;
T(int x);
private:
int t;
};

T::T(int x): t(x > 0? STATIC_MEMBER_A: STATIC_MEMBER_B) {}

int main(int argnum, char** args)
{
T t(-5);
}

//end file test.cpp

I'm getting following reproducable error in linkage:
/tmp/cc0UEe2g.o: In function `T::T(int)':
test.cpp:(.text+0x13): undefined reference to `T::STATIC_MEMBER_A'
test.cpp:(.text+0x1e): undefined reference to `T::STATIC_MEMBER_B'
/tmp/cc0UEe2g.o: In function `T::T(int)':
test.cpp:(.text+0x43): undefined reference to `T::STATIC_MEMBER_A'
test.cpp:(.text+0x4e): undefined reference to `T::STATIC_MEMBER_B'
collect2: ld returned 1 exit status

I'm using g++ (GCC) 4.1.2 20061115 (prerelease) (Debian 4.1.1-21).

If I change
T::T(int x): t(x > 0? STATIC_MEMBER_A: STATIC_MEMBER_B) {}
to
T::T(int x): t(x > 0? STATIC_MEMBER_A: 9) {}
the program can be linked and executed without any error.

Is this a bug in g++? Does anybody have a clue?

Have you tried the FAQ? Static data members need to be defined
if they are used outside of the class definition. Had you put
the entire constructor inside the class definition, you might get
away without defining your static data members, the Standard
allows it. Try it and see if your compiler is advanced enough.
If you _have_ to have the constructor defined outside, then you
have no choice but to define your static data members outside too
and don't give them any initialisers since they already have those
in the class definition.


V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top