question about static variables

S

sieg1974

Hi,

I have this following class, but when I compile it, gcc shows a error
message "undefined reference to `Animal::theNumberOfAnimals`".
I know this must be very easy to fix, but for a newnie like me it's
not :p

Thanks in advance,

Andre



class Animal
{
private:
char theName[ 128 ];
static int theNumberOfAnimals;

public:
Animal()
{
theNumberOfAnimals++;
};

Animal( char * name )
{
strcpy( theName, name );
theNumberOfAnimals++;
};

~Animal(){};
};
 
J

John Carson

sieg1974 said:
Hi,

I have this following class, but when I compile it, gcc shows a error
message "undefined reference to `Animal::theNumberOfAnimals`".
I know this must be very easy to fix, but for a newnie like me it's
not :p

Thanks in advance,

Andre



class Animal
{
private:
char theName[ 128 ];
static int theNumberOfAnimals;

public:
Animal()
{
theNumberOfAnimals++;
};

Animal( char * name )
{
strcpy( theName, name );
theNumberOfAnimals++;
};

~Animal(){};
};

static variables are like member functions in that you declare them in the
class declaration and then define them outside it. Typically, the definition
is done in a .cpp file rather than a header file since (like a function
definition) it must be done only once. Just add:

int Animal::theNumberOfAnimals;

in a .cpp file (note that you must NOT use the static keyword in the
definition).
 
D

Dan Bloomquist

sieg1974 said:
class Animal
{
private:
static int theNumberOfAnimals;
};


you need to define/( and should initailize) static members. In the
appropriate cpp file:

int Animal::theNumberOfAnimals= 0;

And it works even if private!

Best, Dan.
 
T

The King of Pots and Pans

Animal()
{
theNumberOfAnimals++;
};

Animal( char * name )
{
strcpy( theName, name );
theNumberOfAnimals++;
};

~Animal(){};

What's with the semi-colons after the last curly brace of each
function?
 
D

David Harmon

On Wed, 21 Apr 2004 07:11:38 GMT in comp.lang.c++, The King of Pots and
Pans said:
What's with the semi-colons after the last curly brace of each
function?

It's a handy place to store spares, so if you forget a semicolon
elsewhere, as often happens, you can just grab one from nearby
and place it where needed. Sometimes you will see three or four
semicolons after a function just for that reason.
 
R

Rakesh Kumar

David said:
On Wed, 21 Apr 2004 07:11:38 GMT in comp.lang.c++, The King of Pots and



It's a handy place to store spares, so if you forget a semicolon
elsewhere, as often happens, you can just grab one from nearby
and place it where needed.
Is that a suggested style of writing it or is it that only novices
use it. IMHO, it degrades readability of the code.
 
D

David Harmon

On Wed, 21 Apr 2004 07:11:38 GMT in comp.lang.c++, The King of Pots and



It's a handy place to store spares, so if you forget a semicolon
elsewhere, as often happens, you can just grab one from nearby
and place it where needed.
Is that a suggested style of writing it or is it that only novices
use it. IMHO, it degrades readability of the code.[/QUOTE]

It's a joke. You don't need to store spare semicolons.
 

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

No members online now.

Forum statistics

Threads
474,166
Messages
2,570,901
Members
47,442
Latest member
KevinLocki

Latest Threads

Top