delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/10/22/11:12:35

From: Roland Exler <R DOT Exler AT jk DOT uni-linz DOT ac DOT at>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Undefined reference to static members
Date: Tue, 22 Oct 1996 08:14:59 -0700
Organization: Institute for el. Measurement, University of Linz, Austria
Lines: 38
Message-ID: <326CE4F3.5D28@jk.uni-linz.ac.at>
References: <326BE714 DOT 3019E999 AT mnar DOT tky DOT hut DOT fi>
NNTP-Posting-Host: sensor4.emt.uni-linz.ac.at
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Tero Parvinen wrote:

> I can't get djgpp to recognize static member variables. If I do a
> 'gcc -c file.cc' everything seems to run fine, but if I try to link the
> object files, I get messages complaining about undefined references.

> class CTest
> {
>  public:
>     static int a;
>     static void SSetA(int p);
>     void SetA(int p);
>     int GetA();
> };

> stest.cc(.text+0x7): undefined reference to `CTest::a'
> What's wrong here? Is it a bug or am I doing something wrong?

static members of classes are just defined in the class, but has to be declared 
outside the class. Add 

  static int CTest::a;

outside the class-definition to create the variable. If you want, you can init 
it as well at this place (I think you want to do this with your call 
'CTest::SSetA(1)' in main) using 

  static int CTest::a=1; 

Roland

+---------------------------------------+---------------------------+
I Roland Exler                          I EMAIL:                    I
I Universitaet Linz                     I R DOT Exler AT jk DOT uni-linz DOT ac DOT at I
I Institut fuer Elektrische Messtechnik I                           I
I Altenbergerstr. 69                    I Phone:                    I
I A-4040 Linz, AUSTRIA                  I + 43 732 2468 9205        I
+---------------------------------------+---------------------------+

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019