🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: gnu.g++.help
Article #46

Re: static vals inside a class are unresolved (g++ 2.0/SunOS4.1.1)

#46
From: Tal Lewis Lancaster
Date: Thu, 09 Apr 1992 23:49
55 lines
2398 bytes
mccrory@tomato.fnal.gov (Elliott McCrory) writes:

>How has the use of "static" changed since g++ 1.39?  I have a
>situation like this:

>% cat world.cxx
>#include <std.h>
>#include <stream.h>

>class Foo {
>  static int a;
> public:
>  Foo(int c) { a=c ; f=0.0; };
>  int b() { return a; };
>  static float f;
>};

// Explicit declarations for static members:
int Foo::a;
float Foo::f;


>int main(int argc, char* argv[])
>{
>  Foo A(1);
>  cout << A.b() << "Hello world\n" << A.f << "\n";
>}

>% make -k world
>g++  -g -Dsun4 -DEBUG -o world world.cxx
>world.cxx:7 (world.o): Undefined symbol __3Foo$a referenced from text segment
>world.cxx:7 (world.o): Undefined symbol __3Foo$a referenced from text segment
>world.cxx:7 (world.o): Undefined symbol __3Foo$f referenced from text segment
>world.cxx:7 (world.o): Undefined symbol __3Foo$f referenced from text segment
>world.cxx:8 (world.o): Undefined symbol __3Foo$a referenced from text segment
>world.cxx:8 (world.o): Undefined symbol __3Foo$a referenced from text segment
>world.cxx:15 (world.o): Undefined symbol __3Foo$f referenced from text segment
>world.cxx:15 (world.o): Undefined symbol __3Foo$f referenced from text segment
>*** Error code 1
>make: Warning: Target `world' not remade because of errors

The problem is that all static members must be declared outside of the
class scope. I don't know if they need to have global linkage but that is
usually how I do it. But the code that I added above should work

>Compilation finished at Thu Apr  9 16:13:14

>Thanks in advance.
>--
>  ||    Elliott McCrory, Ph. D.		| mccrory@tomato.fnal.gov (internet)
>-/  \-  Fermi National Accelerator Lab	| ALMOND::MCCRORY	 (DECnet)
>-\  /-  Batavia, IL  60510		| MCCRORY@FNALAD	 (BITnet)
>  ||    708-840-4808			| linac!tomato!mccrory	 (uucp)

Tal Lancaster
Technical Details
Message-ID: <1992Apr9.234910.7629@cco.caltech.edu>
Path: arc1-ca.pugleaf.net!sparky!uunet!usc!elroy.jpl.nasa.gov!news.claremont.edu!nntp-server.caltech.edu!tll
References: <MCCRORY.92Apr9161630@tomato.fnal.gov>

Show Full Headers
Complete Article Headers
Newsgroups: gnu.g++.help
Path: arc1-ca.pugleaf.net!sparky!uunet!usc!elroy.jpl.nasa.gov!news.claremont.edu!nntp-server.caltech.edu!tll
From: tll@cco.caltech.edu (Tal Lewis Lancaster)
Subject: Re: static vals inside a class are unresolved (g++ 2.0/SunOS4.1.1)
Message-ID: <1992Apr9.234910.7629@cco.caltech.edu>
Sender: news@cco.caltech.edu
Nntp-Posting-Host: jomby
Organization: California Institute of Technology, Pasadena
References: <MCCRORY.92Apr9161630@tomato.fnal.gov>
Distribution: gnu
Date: Thu, 9 Apr 1992 23:49:10 GMT
Lines: 56