help-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: undefined error message for static variable during link phase


From: Paul Pluzhnikov
Subject: Re: undefined error message for static variable during link phase
Date: 29 Jan 2005 15:18:04 -0800
User-agent: Gnus/5.0808 (Gnus v5.8.8) XEmacs/21.4 (Artificial Intelligence)

jk <joerg-at-ibk@web.de> writes:

> struggling with C++ I run into an error which I do not understand.
> see the attached files

Again you should have tried to make a reduced test case:

    struct Foo { static int x; };
    int main()  { Foo f; return f.x; }

> the static variable foo1 is being declared undefined during link phase.

That's correct: you provided a declaration, but no definition for it.
Fix:

    struct Foo { static int x; };
    int Foo::x = 0; // storage for 'Foo::x' is reserved here (and only here)
    int main()  { Foo f; return f.x; }

> I have an example from "goto Objekt Orientierung" by Herold/Klar/Klar
> (Addison Wesley) which exactly allows this kind of use of a "static"
> variable

Either the example in your book has a bug, or (more likely) you
missed the fact that you must define the static variable as well
as declare it.

Cheers,
-- 
In order to understand recursion you must first understand recursion.
Remove /-nsp/ for email.

reply via email to

[Prev in Thread] Current Thread [Next in Thread]