Connecting Tech Pros Worldwide Forums | Help | Site Map

strange link problem

arcimboldo@gmail.com
Guest
 
Posts: n/a
#1: Mar 7 '06
Hello,

I got a strange link problem; here are the simplest files showing the
error:

a.h::
#include <utility>

class a{
public:
typedef int perm;
static const perm PERM_NONE = 0;
a();
private:
std::pair<int, perm> aaa;
};

a.cpp::
#include "a.h"
a::a() : aaa(0,PERM_NONE) { }
main () { }

Compiling with g++ (GCC) 3.3.5 (Debian 1:3.3.5-13) I get::

$ g++ a.cpp
/tmp/ccKKUXg8.o: In function
`a::a[not-in-charge]()':a.cpp:(.text+0xa): undefined reference to
`a::PERM_NONE'
/tmp/ccKKUXg8.o: In function `a::a[in-charge]()':a.cpp:(.text+0x34):
undefined reference to `a::PERM_NONE'
collect2: ld returned 1 exit status

The file compiles correctly, the error shows up only when linking.

What could it possibly be? Thanks for any help!

Arcimboldo


marcwentink@hotmail.com
Guest
 
Posts: n/a
#2: Mar 7 '06

re: strange link problem


> What could it possibly be?

What if you delete the 'static' keyword?
What if you construct aaa not in the list but in the
compound statement "{}" of the constructor?

My guess is that the static var is not yet initialized when a::a() :
aaa(0,PERM_NONE) { } is called.

Marc Wentink

rmansfield@gmail.com
Guest
 
Posts: n/a
#3: Mar 7 '06

re: strange link problem


The problem is because static data members need to be defined exactly
once in every translation unit. Please see
http://www.parashift.com/c++-faq-lit...html#faq-10.11

Regards,

Ryan Mansfield

Dietmar Kuehl
Guest
 
Posts: n/a
#4: Mar 7 '06

re: strange link problem


rmansfield@gmail.com wrote:[color=blue]
> The problem is because static data members need to be defined exactly
> once in every translation unit.[/color]

Nope! Static data members have to be defined exactly once in the
whole program. The only exception are constant integral expressions
whose address is never taken: these don't need any definition.
However, addresses are easily taken, e.g. by passing the value to a
function taking an 'int const&' as parameter. The best bet for a
constant integral expression which does not need a separate
definition is using an enum.
--
<mailto:dietmar_kuehl@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence
Fei Liu
Guest
 
Posts: n/a
#5: Mar 7 '06

re: strange link problem



arcimboldo@gmail.com wrote:[color=blue]
> Hello,
>
> I got a strange link problem; here are the simplest files showing the
> error:
>
> a.h::
> #include <utility>
>
> class a{
> public:
> typedef int perm;
> static const perm PERM_NONE = 0;
> a();
> private:
> std::pair<int, perm> aaa;
> };
>
> a.cpp::
> #include "a.h"
> a::a() : aaa(0,PERM_NONE) { }[/color]
try a::a(): aaa(0, a::PERM_NONE){}[color=blue]
> main () { }
>
> Compiling with g++ (GCC) 3.3.5 (Debian 1:3.3.5-13) I get::
>
> $ g++ a.cpp
> /tmp/ccKKUXg8.o: In function
> `a::a[not-in-charge]()':a.cpp:(.text+0xa): undefined reference to
> `a::PERM_NONE'
> /tmp/ccKKUXg8.o: In function `a::a[in-charge]()':a.cpp:(.text+0x34):
> undefined reference to `a::PERM_NONE'
> collect2: ld returned 1 exit status
>
> The file compiles correctly, the error shows up only when linking.
>
> What could it possibly be? Thanks for any help!
>
> Arcimboldo[/color]

Closed Thread