C++ static integer class constants...

Alex J. Dam alexjdam@gmail.com
Sun Oct 16 14:50:00 GMT 2005


Hello.

On Sun, Oct 16, 2005 at 02:35:52AM -0500, John Ratliff wrote:
> class foo {
> public:
>   static const int X = 5;
>   static const int Y = 10;
>   static const int Z = 15;
> };
> 
> However, when I went to Linux, where I have g++ 3.3.5 (or possibly 3.3.6,
> but I think it's 3.3.5), the linker would complain about unresolved
> symbols.

I compiled a simple code using such constants here with no problems
using GCC 3.3.4 and 3.4.3.

> If I were to define the static variable in my implementation file, the
> linker would find the variables and go on its merry way. In other words, I
> could solve the problem by doing this:
> 
> const int foo::X;
> const int foo::Y;
> const int foo::Z;

This is usually only needed when, e. g., you take the address of a
constant:

const int *x = &foo::X;

Do you have a minimal sample which does not work?

-- 
Alex. J. Dam



More information about the Gcc-help mailing list