| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"sieg1974" |
| Date: |
20 Apr 2004 10:54:00 PM |
| Object: |
question about static variables |
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(){};
};
.
|
|
| User: "John Carson" |
|
| Title: Re: question about static variables |
20 Apr 2004 11:03:40 PM |
|
|
"sieg1974" <sieg1974@yahoo.com> wrote in message
news:897326d0.0404201954.2cacb08f@posting.google.com
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).
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)
.
|
|
|
|
| User: "The King of Pots and Pans" |
|
| Title: Re: question about static variables |
21 Apr 2004 02:11:38 AM |
|
|
On Wed, 21 Apr 2004 at 03:54 GMT, sieg1974 spoke:
Animal()
{
theNumberOfAnimals++;
};
Animal( char * name )
{
strcpy( theName, name );
theNumberOfAnimals++;
};
~Animal(){};
What's with the semi-colons after the last curly brace of each
function?
--
The King of Pots and Pans
.
|
|
|
| User: "David Harmon" |
|
| Title: Re: question about static variables |
21 Apr 2004 01:37:55 PM |
|
|
On Wed, 21 Apr 2004 07:11:38 GMT in comp.lang.c++, The King of Pots and
Pans <King@ask.for.email.invalid> wrote,
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.
.
|
|
|
| User: "Rakesh Kumar" |
|
| Title: Re: question about static variables |
21 Apr 2004 05:39:11 PM |
|
|
David Harmon wrote:
On Wed, 21 Apr 2004 07:11:38 GMT in comp.lang.c++, The King of Pots and
Pans <King@ask.for.email.invalid> wrote,
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.
Is that a suggested style of writing it or is it that only novices
use it. IMHO, it degrades readability of the code.
--
Rakesh Kumar
** Remove nospamplz from my email address for my real email **
.
|
|
|
| User: "David Harmon" |
|
| Title: Re: question about static variables |
22 Apr 2004 12:32:50 AM |
|
|
On Wed, 21 Apr 2004 15:39:11 -0700 in comp.lang.c++, Rakesh Kumar
<dreamzlion_nospamplz@yahoo.com> wrote,
David Harmon wrote:
On Wed, 21 Apr 2004 07:11:38 GMT in comp.lang.c++, The King of Pots and
Pans <King@ask.for.email.invalid> wrote,
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.
Is that a suggested style of writing it or is it that only novices
use it. IMHO, it degrades readability of the code.
It's a joke. You don't need to store spare semicolons.
.
|
|
|
|
|
|
|
| User: "Dan Bloomquist" |
|
| Title: Re: question about static variables |
20 Apr 2004 11:14:10 PM |
|
|
sieg1974 wrote:
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.
--
http://lakeweb.net
http://ReserveAnalyst.com
No EXTRA stuff for email.
.
|
|
|
|

|
Related Articles |
|
|