🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: gnu.gcc.help
Article #208

static instance variables in class not visible

#208
From: Mark Fausett
Date: Fri, 31 Jul 1992 18:28
53 lines
1410 bytes
Curious;  If I declare a static instance variable in a class, for example:
//class.H:
class Stat {
protected:
  int nonStat;
  static int isStat;
public:
  Stat(int n, int s){
    nonStat=n;
    isStat=s;
  }
  int getStatVar();
  void setStatVar(int val);
};

and then in class.C attempt to use it:

//class.C
#include "class.H"

int Stat::getStatVar(){
  return isStat;
}

void Stat::setStatVar(int val){
  isStat = val;
}

#include <iostream.h>
main(int argc, char *argv[]){
  Stat j(1,2);
  Stat k(1,5);
  cout << j.getStatVar() <<endl;
  j.setStatVar(4);
  cout << k.getStatVar() <<endl;
}

I get linking problems like this:

gcc -o class class.C -lg++ld: Undefined symbol 
   __4Stat$isStat 
collect: /usr/bin/ld returned 2 exit status

Compilation exited abnormally with code 1 at Fri Jul 31 11:35:32

In particular, I'm not use GNU ld.  This is on a Sun4m, but same thing 
happens on my NeXT as well.
Do I misunderstand static class variables, will it work with GNU ld, or ???

Thanks!

Mark Fausett
mfausett@bbn.com 
Technical Details
Message-ID: <mfausett.712607301@kirin>
Path: arc1-ca.pugleaf.net!sparky!uunet!elroy.jpl.nasa.gov!usc!news.bbn.com!kirin!mfausett

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!elroy.jpl.nasa.gov!usc!news.bbn.com!kirin!mfausett
From: mfausett@bbn.com (Mark Fausett)
Newsgroups: gnu.gcc.help
Subject: static instance variables in class not visible
Date: 31 Jul 92 18:28:21 GMT
Lines: 53
Distribution: world
Message-ID: <mfausett.712607301@kirin>
NNTP-Posting-Host: kirin.bbn.com