473,480 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

gcc linker won't handle class member

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
3 1457
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2233
by: adeht | last post by:
In his book, "Modern C++ Design", Andrei Alexandrescu presents a Generic Object Factory class (Loki::Factory). One of its advantages (also mentioned in the book) is being able to register the...
4
4867
by: Serge | last post by:
Hi, I have no problem creating a static member variable with integers, etc but when I try the same with a vector then I always get linker errors that the static member variable is unknown...
12
1946
by: Fabio De Francesco | last post by:
Hello. I can't understand why I can't compile the following simple code, where I think I have applied all the needed rules for templates that are declared and defined in different files (*.h and...
2
3009
by: Indiana Epilepsy and Child Neurology | last post by:
Before asking this questions I've spent literally _years_ reading (Meyer, Stroustrup, Holub), googling, asking more general design questions, and just plain thinking about it. I am truly unable to...
4
2045
by: Roland Zerek | last post by:
Hi, Is there any way to avoid the compiler/linker to optimize one (and only this) class member? Below is the code: <code> class MyFactory { /* . . . */ public static void Register(...
2
2104
by: Joske | last post by:
Hi, I'm having a similar problem as Scott. A static library with managed C++ functions fails to link in. See http://groups.google.be/groups?q=managed+C%2B%...
1
1747
by: toton | last post by:
In C++ , when the class is declared in the header & not implemented in the cpp, the linker is not giving a error untill the method gets called. How to ensure that all non pure-virtual method...
3
2654
by: little.freaky | last post by:
Hello group, I have a problem with template classes and inheritance. I've searched on the internet to find a solution but all the examples look the same as my code (as far as I can tell) and I...
3
3127
by: Peterwkc | last post by:
Hello all C++ expert programmer, I have a handle class which point to another class and use the pointer as object. I follow the code from C++ articles submited by someone in this forum....
0
7037
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6904
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7032
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6730
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
6873
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5321
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4767
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
2990
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
558
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.