C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

gcc linker won't handle class member

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Karl Heinz Buchegger
Guest





PostPosted: Mon Aug 30, 2004 2:44 pm    Post subject: Re: gcc linker won't handle class member Reply with quote



Sigmund Skjelnes wrote:
Quote:

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
[email]kbuchegg (AT) gascad (DOT) at[/email]

Back to top
Peter Kragh
Guest





PostPosted: Mon Aug 30, 2004 2:45 pm    Post subject: Re: gcc linker won't handle class member Reply with quote



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)
Quote:

// #include #include
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

Back to top
Sigmund Skjelnes
Guest





PostPosted: Mon Aug 30, 2004 4:43 pm    Post subject: gcc linker won't handle class member Reply with quote



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 %dn", id );
}

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

~T(){
printf( "Destructor kalt: %dn", 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: %dn", 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: %in", t.number );

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

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

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

void setPNumber( T *t, int n){

t->number = n;
}

Back to top
Sigmund Skjelnes
Guest





PostPosted: Mon Aug 30, 2004 5:12 pm    Post subject: Re: gcc linker won't handle class member Reply with quote

Sigmund Skjelnes wrote:
Quote:
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

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.