static data-member treated as extern

  • Thread starter Scott J. McCaughrin
  • Start date
S

Scott J. McCaughrin

The following short program compiles fine but fails to link (both on
Borland C++ and GNU g++). In particular, g++ gives this link error:
"undefined reference to `VarArrar::funct'".

It seems as if the static data-member (of type Funct) is regarded as
an external, hence unresolvable by 'ld'. If so, why is a static member
considered an extern, and what is the remedy?
 
V

Victor Bazarov

Scott said:
The following short program

What short program?
compiles fine but fails to link (both on
Borland C++ and GNU g++). In particular, g++ gives this link error:
"undefined reference to `VarArrar::funct'".

It seems as if the static data-member (of type Funct) is regarded as
an external, hence unresolvable by 'ld'. If so, why is a static member
considered an extern, and what is the remedy?

Why? Because it is. The remedy is to define it.

V
 
J

John Harrison

Scott J. McCaughrin said:
The following short program compiles fine but fails to link (both on
Borland C++ and GNU g++). In particular, g++ gives this link error:
"undefined reference to `VarArrar::funct'".

It seems as if the static data-member (of type Funct) is regarded as
an external, hence unresolvable by 'ld'. If so, why is a static member
considered an extern, and what is the remedy?

If it were not then every source file in which you included the class
definition would get a separate definition of the static data member,
resulting in multiple definition errors.

The remedy is the same as with globals, define the static data member in one
source file

Funct SomeClass::some_member = something;

john
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top