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 09:00:00 -0700 (PDT)
User-agent: G2/1.0

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.

Interesting...that does fix the problem, but doesn't explain why it
works on one line but not the other.

I guess I could speculate that in the cases it works without the
definition the compiler is able to insert the constant value
directly.  The std::replace algorithm wants a reference to a variable,
so the definition is required.  Though it seems inconsistent, I can
see how this could happen since you don't know until link time whether
a definition exists.  Although, other compilers don't have a problem
with it, either - not sure where they're sticking the actual
definition since in my real case the declaration is in a header file.

Thanks,
Cary


reply via email to

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