delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/06/03/08:49:49

From: johne AT parallax DOT co DOT uk (John Eccleston)
Newsgroups: comp.os.msdos.djgpp
Subject: [Q] Static class member variables
Date: Tue, 03 Jun 97 10:48:54 GMT
Organization: Parallax Solutions Ltd
Message-ID: <865334760.165475@red.parallax.co.uk>
NNTP-Posting-Host: red.parallax.co.uk
Cache-Post-Path: red.parallax.co.uk!unknown AT parsnip DOT parallax DOT co DOT uk
Lines: 68
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Hi All,

I have just been experimenting with C++ and have tried using the concept
of static class member variables, which according to my copy of
'The C++ Programming Language' 2ed by Bjarne Stroustrup is valid C++ syntax

-----> Example <-----
#include <stdio.h>

class clsTest
{
public :
   static int instanceCount = 0;

public :
   clsTest() { instanceCount++; };
   ~clsTest() { instanceCount--; };
   
   virtual int getInstanceCount(void) { return instanceCount; };
};

main(int argc, char *argv[])
{
   clsTest
      *test;

   printf("Instances (before) %d\n", clsTest::instanceCount);
   test = new clsTest();

   printf("Instances: %d\n", test->getInstanceCount());
   
   delete test;
}
-----> Example <-----

However when I compile the example with gxx test.c -o test.exe, I get what 
I think are LD errors:

-----> Errors <-----
C:\Temp>gxx test.cc -o test.exe
c:/djgpp/tmp\ccdaaaaa(.text+0x35):test.cc: undefined reference to 
`clsTest::instanceCount'
c:/djgpp/tmp\ccdaaaaa(.text+0xc7):test.cc: undefined reference to 
`clsTest::instanceCount'
c:/djgpp/tmp\ccdaaaaa(.text+0xe7):test.cc: undefined reference to 
`clsTest::instanceCount'
c:/djgpp/tmp\ccdaaaaa(.text+0x116):test.cc: undefined reference to 
`clsTest::instanceCount'
-----> Errors <-----

Could anybody explain what is happening here.  I have looked in the FAQ
but couldn't spot anything.

Thanks in advance

John

________________________________________________________________
Parallax Solutions Ltd.   Tel.: 01203 514522
Stonecourt,               Fax.: 01203 514401
Siskin Drive,             Web : http://www.parallax.co.uk/~johne
Coventry CV3 4FJ          Mail: johne AT parallax DOT co DOT uk
________________________________________________________________
                         Remember:
 There is no such thing as computer error, it's 100% YOUR fault
________________________________________________________________


- Raw text -


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