Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > gcc linker won't handle class member

Reply
Thread Tools

gcc linker won't handle class member

 
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      08-30-2004
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-lit...html#faq-10.10

--
Karl Heinz Buchegger
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
 
Peter Kragh
Guest
Posts: n/a
 
      08-30-2004
Sigmund Skjelnes wrote:

You need to *define* the static member variable also (see
http://www.parashift.com/c++-faq-lit...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
 
Reply With Quote
 
 
 
 
Sigmund Skjelnes
Guest
Posts: n/a
 
      08-30-2004
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;
}

 
Reply With Quote
 
Sigmund Skjelnes
Guest
Posts: n/a
 
      08-30-2004
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
 
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
gcc 4 can not resolve member var derived from base class in atemplate class steve yee C++ 5 07-01-2008 09:31 PM
Static class member Linker Errors hyling.s1@gmail.com C++ 2 08-09-2006 09:38 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
parse error in gcc but success in vc.net, call a non_template class's template member function from a template class's member function! ken C++ 2 06-28-2005 06:57 AM
gcc linker problem Georg C Programming 3 07-10-2003 04:47 PM



Advertisments