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?
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?