By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,750 Members | 1,214 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,750 IT Pros & Developers. It's quick & easy.

gcc linker won't handle class member

P: n/a
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;
}

Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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
kb******@gascad.at
Jul 22 '05 #2

P: n/a
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
Jul 22 '05 #3

P: n/a
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
Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.