L'il help with static member please?

L

Lilia

Hi All,



I have a class like this:
//================================
class foo
{
public:

static foo* foo_ptr;

void bar();
}
//================================


Then I have a member function like this:
//================================
void foo::bar()
{
foo_ptr = NULL;
}
//================================


The linker is telling me:
//================================
unresolved external symbol "public: static class foo * foo::foo_ptr" (?
foo_ptr@foo@@2PAV1@A)
//================================

Any ideas on what I'm doing wrong?
Everything is fine if I move the foo_ptr out of the class and below the
class definition in the header file, but I'd like it to be a member.


Thanks,

-L
 
R

Russell Hanneken

Lilia said:
I have a class like this:
//================================
class foo
{
public:
static foo* foo_ptr;
void bar();
}

Don't forget the semicolon following the class definition.
//================================

Then I have a member function like this:
//================================
void foo::bar()
{
foo_ptr = NULL;
}
//================================

The linker is telling me:
//================================
unresolved external symbol "public: static class foo * foo::foo_ptr" (?
foo_ptr@foo@@2PAV1@A)
//================================

This is addressed in the FAQ:

http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.10
 
R

Rob Williscroft

Lilia wrote in
//================================
class foo
{
public:

static foo* foo_ptr;

void bar();
}
//================================

The above is only a declaration, you also need a defenition:

foo * foo::foo_ptr = NULL;

Your linker should now be able to find:
unresolved external symbol "public: static class foo * foo::foo_ptr" (?
foo_ptr@foo@@2PAV1@A)

HTH.

Rob.
 
L

Lilia

Wow, and just when I was getting pretty cocky about my C++ knowledge. :)

It'll be interesting to pull out the 'ol Stroustrup and figure out why
the 2-step (why can't it just be assumed to be a definition as well, is
there a time when you wouldn't do both steps?).


Thanks for the help.


-Lilia

PS. Thanks to Russell for the http://www.parashift.com/c++-faq-lite/
link
 
R

Rob Williscroft

Lilia wrote in
Wow, and just when I was getting pretty cocky about my C++ knowledge. :)

It'll be interesting to pull out the 'ol Stroustrup and figure out why
the 2-step (why can't it just be assumed to be a definition as well, is
there a time when you wouldn't do both steps?).

Well if it was a defenition in a header (as such declaration's often
are) then included by 2 source files, you would get a duplication
problem. Also where does the initializer go and which source file
compiles the initializer ?

// header
#include <cmath>

struct X
{
static double sqrt2;
};

//source file
double X::sqrt2 = std::sqrt( 2.0 );

Rob.
 
S

Sharad Kala

Lilia said:
Wow, and just when I was getting pretty cocky about my C++ knowledge. :)

It'll be interesting to pull out the 'ol Stroustrup and figure out why
the 2-step (why can't it just be assumed to be a definition as well, is
there a time when you wouldn't do both steps?).
I view it this way.
Static objects are specific to a class and shared by all objects of the class.
Whenever you instantiate objects of a class each object gets it's due of member
variables.
But what about the static instance?
It can't get defined with each object, so it's your duty now to do so separately
in an implementation file.

-Sharad
 
L

Lilia

Ahh,
Makes sense.

Too bad it can't just be assumed that the first time it's seen by the
compiler it's initialized and everything else is just redundant.

-Lilia
 

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
474,160
Messages
2,570,889
Members
47,422
Latest member
LatashiaZc

Latest Threads

Top