Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Linkage problems with in-class static members

Reply
Thread Tools

Linkage problems with in-class static members

 
 
Nico
Guest
Posts: n/a
 
      02-08-2008
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?
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      02-08-2008
Nico wrote:
> 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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Thread safety problems with function scope static variables vs class static private members Hicham Mouline C++ 5 12-19-2008 08:10 PM
c++ linkage vs c linkage ramasubramanian.rahul@gmail.com C++ 1 09-12-2008 11:41 AM
Difference between static final members and final static members(if any)? JFCM Java 4 02-07-2006 11:32 AM
Problem: shared object loading runs constructor of a static object, but static linkage does not. tropos C++ 3 11-30-2005 04:54 PM
Static classes with static members Ben ASP .Net 3 06-01-2004 07:43 PM



Advertisments