Linkage problems with in-class static members

Discussion in 'C++' started by Nico, Feb 8, 2008.

  1. Nico

    Nico Guest

    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?
    Nico, Feb 8, 2008
    #1
    1. Advertising

  2. 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
    Victor Bazarov, Feb 8, 2008
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. SaravanaKumar
    Replies:
    6
    Views:
    9,358
    Tony Morris
    Oct 19, 2004
  2. JFCM
    Replies:
    4
    Views:
    5,720
  3. tropos
    Replies:
    3
    Views:
    447
  4. Replies:
    1
    Views:
    579
    Michael DOUBEZ
    Sep 12, 2008
  5. Hicham Mouline
    Replies:
    5
    Views:
    2,342
    James Kanze
    Dec 19, 2008
Loading...

Share This Page