gcc linker won't handle class member

Discussion in 'C++' started by Karl Heinz Buchegger, Aug 30, 2004.

  1. Sigmund Skjelnes wrote:
    >
    > Hi!
    > Made some small sample program, it's a very long since. The linker won't
    > handle a static member for any reason, it came up with "unassigned
    > reference to T::idcnt". idcnt is a static member of T, see attached
    > source file, note that I'd have to use a global variable to get the
    > program running, the original idcnt is commented out. There are some
    > Norwegian text in the source, I'd think the program is readable anyway.
    >
    > Now I'd hope somebody have any idea what is wrong.


    That's a FAQ

    http://www.parashift.com/c -faq-lite/ctors.html#faq-10.10

    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Aug 30, 2004
    #1
    1. Advertising

  2. Karl Heinz Buchegger

    Peter Kragh Guest

    Sigmund Skjelnes wrote:

    You need to *define* the static member variable also (see
    http://www.parashift.com/c -faq-lite/ctors.html#faq-10.10)
    >
    > // #include<iostream.h>
    > #include<stdio.h>
    >
    > static int idcnt; // se T
    >
    > class T
    > {
    > public:
    > //static int idcnt; // denne liker ikke linkeren
    > int id;
    >
    > int number;
    >

    <snip>

    int T::idcnt = 0; //defined outside tha class definition.

    HTH.

    BR,
    Peter
     
    Peter Kragh, Aug 30, 2004
    #2
    1. Advertising

  3. Hi!
    Made some small sample program, it's a very long since. The linker won't
    handle a static member for any reason, it came up with "unassigned
    reference to T::idcnt". idcnt is a static member of T, see attached
    source file, note that I'd have to use a global variable to get the
    program running, the original idcnt is commented out. There are some
    Norwegian text in the source, I'd think the program is readable anyway.

    Now I'd hope somebody have any idea what is wrong.

    Cincerely,
    Sigmund

    // #include<iostream.h>
    #include<stdio.h>

    static int idcnt; // se T

    class T
    {
    public:
    //static int idcnt; // denne liker ikke linkeren
    int id;

    int number;


    T( int n ){
    number = n;
    id = idcnt++;
    printf( "Constructor kalt %d\n", id );
    }

    T( const T &t ){
    number = t.number;
    id = idcnt++;
    printf( "Kopierings-constructor: %d\n", id );
    }

    ~T(){
    printf( "Destructor kalt: %d\n", id );
    }

    void setNumber( int n ){
    number = n;
    }
    };

    void setNumber( T t, int n );
    void setPNumber( T *t, int n );

    // finner iostream.h, men ikke det tilhørende bibloteket

    void main(){

    T t(-1);
    // cout << "Constructor setter number til -1 " << t.number << endl;
    printf( "Constructor setter number til -1: %d\n", t.number );

    setNumber( t, 4 );
    // cout << "Instans gitt ved referanse endrer ikke instansen ";
    // cout << t.number << endl;
    printf( "Instans gitt ved referanse endrer ikke instansen: %i\n", t.number );

    setPNumber( &t, 4 );
    // cout << "Instans gitt med peker endrer instansen: ";
    // cout << t.number << endl;
    printf( "Instans gitt med peker endrer instansen: %i\n", t.number );

    t.setNumber( 6 );
    //cout << "Medlemsfunksjon endrer instansen " << t.number << endl;
    printf( "Medlemsfunksjon endrer instansen %i\n", t.number );
    }

    void setNumber( T t, int n ){
    t.number = n;
    printf("Her kalles kopi-constructor for å kopiere instansen\n");
    }

    void setPNumber( T *t, int n){

    t->number = n;
    }
     
    Sigmund Skjelnes, Aug 30, 2004
    #3
  4. Sigmund Skjelnes wrote:
    > Hi!
    > Made some small sample program, it's a very long since. The linker won't
    > handle a static member for any reason, it came up with "unassigned
    > reference to T::idcnt". idcnt is a static member of T, see attached
    > source file, note that I'd have to use a global variable to get the
    > program running, the original idcnt is commented out. There are some
    > Norwegian text in the source, I'd think the program is readable anyway.
    >
    > Now I'd hope somebody have any idea what is wrong.
    >
    > Cincerely,
    > Sigmund
    >


    <snip>

    That helped! Thanks! And: I'd have bookmarked the FAQ page.
    Sigmund
     
    Sigmund Skjelnes, Aug 30, 2004
    #4
    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. E11
    Replies:
    1
    Views:
    4,941
    Thomas Weidenfeller
    Oct 12, 2005
  2. Georg

    gcc linker problem

    Georg, Jul 10, 2003, in forum: C Programming
    Replies:
    3
    Views:
    791
    Dan Pop
    Jul 10, 2003
  3. Steven

    GCC linker throws weird message.

    Steven, Feb 25, 2005, in forum: C Programming
    Replies:
    9
    Views:
    371
    Kelsey Bjarnason
    Mar 2, 2005
  4. Replies:
    2
    Views:
    311
    Victor Bazarov
    Aug 9, 2006
  5. steve yee
    Replies:
    5
    Views:
    372
    Bo Persson
    Jul 1, 2008
Loading...

Share This Page