468,457 Members | 1,643 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,457 developers. It's quick & easy.

using static variables

Hi, I'm having some linker issues when using static variables in a
class.

class TestClass
{
public:

TestClass()
{
Count++;
}

private:
static int Count;
};

int main(int argc, char *argv[])
{
TestClass T;
return EXIT_SUCCESS;
}
The above gives me an undefined reference (to TestClass::Count) error
during linking. How can I get this to work without having to put my
function definitions in a cpp file?

Thanks

Sep 12 '06 #1
7 3143
jo***********@gmail.com wrote:
Hi, I'm having some linker issues when using static variables in a
class.

class TestClass
{
public:

TestClass()
{
Count++;
}

private:
static int Count;
};

int main(int argc, char *argv[])
{
TestClass T;
return EXIT_SUCCESS;
}
The above gives me an undefined reference (to TestClass::Count) error
during linking. How can I get this to work without having to put my
function definitions in a cpp file?
With static data members, you must declare them inside the class (as
you have done) and then define/initialize them somewhere outside the
class (as you have neglected to do). For instance, you could write:

int TestClass::Count = 0;

before or after the definition of main().

Cheers! --M

Sep 12 '06 #2
With static data members, you must declare them inside the class (as
you have done) and then define/initialize them somewhere outside the
class (as you have neglected to do). For instance, you could write:

int TestClass::Count = 0;

before or after the definition of main().
What if I had the class definition in a separate file, say testclass.h,
which included the following:

//-- begin testclass.h
#ifndef TESTCLASSH
#define TESTCLASSH

class TestClass
{
private:
static int Count;

public:

TestClass()
{
Count++;
}
};
int TestClass::Count = 0;

#endif
// --- end testclass.h
and main() in a separate file, say main.cpp:

//-- begin main.cpp
#include <testclass.h>

int main(int argc, char *argv[])
{
TestClass t;
return EXIT_SUCCESS;
}
// --- end main.cpp

I get a multiple definitions of TestClass::Count error, but I want the
initialization of Count to take place in testclass.h, is this possible?

Sep 12 '06 #3
jo***********@gmail.com wrote:
>With static data members, you must declare them inside the class (as
you have done) and then define/initialize them somewhere outside the
class (as you have neglected to do). For instance, you could write:

int TestClass::Count = 0;

before or after the definition of main().

What if I had the class definition in a separate file, say
testclass.h, which included the following:

//-- begin testclass.h
#ifndef TESTCLASSH
#define TESTCLASSH

class TestClass
{
private:
static int Count;

public:

TestClass()
{
Count++;
}
};
int TestClass::Count = 0;
That's bad. It's an object definition in a header. Never put object
definitions in your headers. It has to go into _one_of_ your translation
units. Which one is usually immaterial.
[..]

I get a multiple definitions of TestClass::Count error, but I want the
initialization of Count to take place in testclass.h, is this
possible?
No, not possible. No initialisation happens in "testclass.h". It's just
another piece of text included in (all or some) of your translation units.
Whatever you have in your "testclass.h" will be _replicated_ in all C++
source files where you write '#include "testclass.h"' (or some such). It
means the headers should only contain _declarations_. Class definitions
are essentially _declarations_. Object definitions don't belong to
headers. Period.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 12 '06 #4
jo***********@gmail.com wrote:
With static data members, you must declare them inside the class (as
you have done) and then define/initialize them somewhere outside the
class (as you have neglected to do). For instance, you could write:

int TestClass::Count = 0;

before or after the definition of main().

What if I had the class definition in a separate file, say testclass.h,
which included the following:

//-- begin testclass.h
#ifndef TESTCLASSH
#define TESTCLASSH

class TestClass
{
private:
static int Count;

public:

TestClass()
{
Count++;
}
};
int TestClass::Count = 0;

#endif
// --- end testclass.h
and main() in a separate file, say main.cpp:

//-- begin main.cpp
#include <testclass.h>

int main(int argc, char *argv[])
{
TestClass t;
return EXIT_SUCCESS;
}
// --- end main.cpp

I get a multiple definitions of TestClass::Count error, but I want the
initialization of Count to take place in testclass.h, is this possible?
You shouldn't get that error here. I suspect you're including the same
header file in a different .cpp file.

In any case, there is no way to do what you want. The static member
must be defined in only one translation unit, and the only way to
control that if you're including the header in multiple .cpp files is
to take it out of the header. The simplest solution is to create a
testclass.cpp file that only #includes your header and defines and
initializes the data. Then no users are responsible for defining it,
but they will need to link in your extra .cpp file.

Cheers! --M

Sep 12 '06 #5
Victor Bazarov wrote:
jo***********@gmail.com wrote:
With static data members, you must declare them inside the class (as
you have done) and then define/initialize them somewhere outside the
class (as you have neglected to do). For instance, you could write:

int TestClass::Count = 0;

before or after the definition of main().
What if I had the class definition in a separate file, say
testclass.h, which included the following:

//-- begin testclass.h
#ifndef TESTCLASSH
#define TESTCLASSH

class TestClass
{
private:
static int Count;

public:

TestClass()
{
Count++;
}
};
int TestClass::Count = 0;

That's bad. It's an object definition in a header. Never put object
definitions in your headers. It has to go into _one_of_ your translation
units. Which one is usually immaterial.
[..]

I get a multiple definitions of TestClass::Count error, but I want the
initialization of Count to take place in testclass.h, is this
possible?

No, not possible. No initialisation happens in "testclass.h". It's just
another piece of text included in (all or some) of your translation units.
Whatever you have in your "testclass.h" will be _replicated_ in all C++
source files where you write '#include "testclass.h"' (or some such). It
means the headers should only contain _declarations_. Class definitions
are essentially _declarations_. Object definitions don't belong to
headers. Period.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

What's the best way of initializing Count then? It just seems inelegant
to have a cpp file devoted entirely to initalizing one static variable.

Sep 12 '06 #6
jo***********@gmail.com wrote:
[..]
What's the best way of initializing Count then? It just seems
inelegant to have a cpp file devoted entirely to initalizing one
static variable.
There is no "best way". There is only _one_ way; the question can
only be "where". But the problem is (for some) that *any* cpp file
would do.

Just pick one, and forget we ever had this conversation.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 12 '06 #7
On 12 Sep 2006 11:13:04 -0700 in comp.lang.c++,
jo***********@gmail.com wrote,
>I get a multiple definitions of TestClass::Count error, but I want the
initialization of Count to take place in testclass.h, is this possible?
There might be some trick to make it possible, but generally: no.
Put the definition in one and only one .cpp file.

This issue is covered in Marshall Cline's C++ FAQ. See the topic
"[10.10] Why are classes with static data members getting linker
errors?" You can get the FAQ at:
http://www.parashift.com/c++-faq-lite/

Sep 12 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by James | last post: by
2 posts views Thread by katekukku | last post: by
9 posts views Thread by Bryan Parkoff | last post: by
18 posts views Thread by Ronald Bruck | last post: by
reply views Thread by Luis Zarrabeitia | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by subhajit12345 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.