473,385 Members | 1,387 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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(){};
};
Jul 22 '05 #1
6 1072
"sieg1974" <si******@yahoo.com> wrote in message
news:89**************************@posting.google.c om
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)

Jul 22 '05 #2


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.

Jul 22 '05 #3
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
Jul 22 '05 #4
On Wed, 21 Apr 2004 07:11:38 GMT in comp.lang.c++, The King of Pots and
Pans <Ki**@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.

Jul 22 '05 #5


David Harmon wrote:
On Wed, 21 Apr 2004 07:11:38 GMT in comp.lang.c++, The King of Pots and
Pans <Ki**@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 **
Jul 22 '05 #6
On Wed, 21 Apr 2004 15:39:11 -0700 in comp.lang.c++, Rakesh Kumar
<dr******************@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 <Ki**@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.

Jul 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Alfonso Morra | last post by:
Hi, I have a function declared thus: int foo( int, int ); In the definition, I want to have a variable that is expensive to create. I want to be able to save the state of this variable...
41
by: GC | last post by:
Within a class, do you use the private keyword for your internal member variables or do you leave it off like you would for variables within a function?
9
by: Neil Kiser | last post by:
I'm trying to understand what defining a class as 'static' does for me. Here's an example, because maybe I am thinking about this all wrong: My app will allows the user to control the fonts...
3
by: juli jul | last post by:
Hello! If I want to pass some class variable from one class to another : how can I do it with the static function? I tried but I can't see the class variables in the static function I made-are...
11
by: dhnriverside | last post by:
Hi peeps Ok, so I thought I'd have a go at making a console app in VS2k5... I haven't written any windows apps for years, let alone dos apps (been web programming) and I've hit a dumb error... ...
4
by: Diffident | last post by:
Hello All, I have a question which is pertinent to Page's lifecycle. I declared a protected static object (global variable within that class) whose value is set only once when the page is...
5
by: admin | last post by:
ok This is my main. Pretty much it goes through each category and starts up 4 worker threads that then ask for groups to gether from. My problem is that when the thread gets done it keeps the...
4
by: =?iso-8859-1?B?Sm9oYW4gU2r2c3Ry9m0=?= | last post by:
It seems that many people experience problems with mix-ups and non-intentional sharing of sessions in ASP.NET. This is often tracked down to the use of static variables for user data, which are...
12
by: Bryan Parkoff | last post by:
I write my large project in C++ source code. My C++ source code contains approximate four thousand small functions. Most of them are inline. I define variables and functions in the global scope....
6
by: vaclavpich | last post by:
Hi, I have a question on constant variables. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.