Thread View: gnu.g++.help
5 messages
5 total messages
Started by Elliott McCrory
Thu, 09 Apr 1992 21:16
static vals inside a class are unresolved (g++ 2.0/SunOS4.1.1)
Author: Elliott McCrory
Date: Thu, 09 Apr 1992 21:16
Date: Thu, 09 Apr 1992 21:16
42 lines
1875 bytes
1875 bytes
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;
};
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
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)
Re: static vals inside a class are unresolved (g++ 2.0/SunOS4.1.1)
Author: Tal Lewis Lancaster
Date: Thu, 09 Apr 1992 23:49
Date: Thu, 09 Apr 1992 23:49
55 lines
2398 bytes
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
Re: static vals inside a class are unresolved (g++ 2.0/SunOS4.1.1)
Author: Elliott McCrory
Date: Fri, 10 Apr 1992 21:17
Date: Fri, 10 Apr 1992 21:17
6 lines
904 bytes
904 bytes
Seems like a kludge. Why has static been abandoned? -- || 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)
Re: static vals inside a class are unresolved (g++ 2.0/SunOS4.1.1)
Author: Joe Buck
Date: Fri, 10 Apr 1992 23:49
Date: Fri, 10 Apr 1992 23:49
53 lines
2185 bytes
2185 bytes
In article <MCCRORY.92Apr10161734@tomato.fnal.gov> mccrory@tomato.fnal.gov (Elliott McCrory) writes:
>Seems like a kludge. Why has static been abandoned?
static has not been abandoned; the change in g++-2.0 is that the
compiler now implements what Ellis and Stroustrup specify in the ARM.
In the example, the code was buggy, even though it compiled and ran
under old g++. The code might or might not have linked successfully
under cfront; it depends on whether the C compiler underneath enforces
the "one definition" rule or not. That is, in some C compilers if
a bunch of different files declare
extern int foo;
then no one has defined foo: it's an undefined symbol. In others,
a definition of foo with value zero will appear. But the ANSI C
standard calls for a definition for each object: a conforming program
has to have "int foo;" somewhere.
In the same way, for each static class variable declared in the class
definition there must exist somewhere an initializer for that variable.
Many compilers kludge one up if none is specified by using the "common
block" technique, but this is contrary to the ARM (think of it as an
extension g++ no longer supports).
If you say
class Bar {
...
};
class Foo {
...
static Bar fooStaticMember;
};
you must, in some source file that includes Foo's definition, say
Bar Foo::fooStaticMember;
If you want to give arguments to the Bar constructor, say
Bar Foo::fooStaticMember(arg1,arg2,...);
If it's an integer, and you want to give a value, say
int Foo::fooStaticInt = 3;
or some such.
--
Joe Buck jbuck@ohm.berkeley.edu
Re: static vals inside a class are unresolved (g++ 2.0/SunOS4.1.1)
Author: Tal Lewis Lancaster
Date: Sat, 11 Apr 1992 00:00
Date: Sat, 11 Apr 1992 00:00
14 lines
1145 bytes
1145 bytes
mccrory@tomato.fnal.gov (Elliott McCrory) writes: >Seems like a kludge. Why has static been abandoned? Well, I got the idea of my so called "kludge" from p. 179 the ARM: "A static member is not part of objects of a class. Static members of a global class have external linkage. The declaration of a static data member in its class declaration is NOT a definition. A definition is required elsewhere." As a side note the "kludge" isn't needed when using ATT 3.0. Tal
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads