help-gplusplus
[Top][All Lists]
Advanced

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

Re: Linker can't find constants


From: Cary
Subject: Re: Linker can't find constants
Date: Wed, 1 Jul 2009 12:20:05 -0700 (PDT)
User-agent: G2/1.0

On Jul 1, 10:00 am, Cary <ca...@juno.com> wrote:
> On Jul 1, 1:04 am, Thomas Maeder <mae...@glue.ch> wrote:
> > Cary <ca...@juno.com> writes:
> > > The following code compiles, but the linker complains of an undefined
> > > reference.  If I comment out the line with *xx* it will link.  Is this
> > > a known and/or fixed bug in later versions of gcc?
>
> > No. It's a bug in your program.
>
> > > SLES 10.2
> > > gcc --version gcc (GCC) 4.1.2 20070115 (SUSE Linux)
>
> > > replace.cpp:
> > > #include <algorithm>
> > > #include <vector>
> > > class A
> > > {
> > > private:
> > >         const static int k1 = 1;
>
> > This just declares k1. It does not define it.
>
> > >         std::vector<int> mv;
> > > public:
> > >         A(int p);
> > > };
>
> > Add
>
> > int const A::k1;
>
> > to define k1.

BTW - this doesn't work for vs.  If you initialize a class constant
with the declaration in a header and also specify a separate
definition in a body VisualStudio 2008 will find multiple definitions
wherever the header is included.  To get it to work for both you have
to do the initialization with the definition and not with the
declaration.  I'd prefer seeing it with the declaration so it is
easily visible in the header file, but I guess a comment will work for
that.  But, now the compiler can't optimize things by just sticking in
the constant since it doesn't know what the value is!

class A
{
  const static int k1 /*= 1*/;

... and later the definition:

int const A::k1 = 1;

Cary



reply via email to

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