473,397 Members | 2,099 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,397 software developers and data experts.

initializing private static members

hi all,
I was trying the following code on the MS .net compiler:

class Item {
static int count;

public:
//static void init() { }

Item()
{
count++;
cout<<"constructing Item"<<endl;
}
~Item()
{
count--;
cout<<"Destroying Item #"<<count<<endl;
}
};

The compiler kept giving me error for the unresloved symbol count.
After that , I made the variable count public and tried to assign 0 to
it in the main function. But still the same error was reported by the
compiler. I am confused about why the error has occured.

Thanks,
Madhav.

Jul 25 '06 #1
14 5898
Madhav wrote:
hi all,
I was trying the following code on the MS .net compiler:

class Item {
static int count;

public:
//static void init() { }

Item()
{
count++;
cout<<"constructing Item"<<endl;
}
~Item()
{
count--;
cout<<"Destroying Item #"<<count<<endl;
}
};

The compiler kept giving me error for the unresloved symbol count.
After that , I made the variable count public and tried to assign 0 to
it in the main function. But still the same error was reported by the
compiler. I am confused about why the error has occured.
Do you have a definition of Item::count in a source file? Sounds like
you have forgotten to do this.

--
Ian Collins.
Jul 25 '06 #2
try to make initialization in this manner in your constructor:
Item() :count(0)
{
count++;
cout<<"constructing Item"<<endl;
}

this manner is called the initializer list.
Note that you can't initialize constant members in a class by =
operator, instead you should use initializer list.
if there are many variables that you want to initialize feel free to
use comma, i.e.
Item():count1(o), count2(3), count3(9)
{
count1++;
count2++;
count3++;
}

Jul 25 '06 #3
luckyboy wrote:
try to make initialization in this manner in your constructor:
Item() :count(0)
No, this is wrong.

If you look back to the code you snipped, count is a static member. It
requires a definition.

--
Ian Collins.
Jul 25 '06 #4
Yes, you must write " int Item::count=0; " in the source file (normal in
the .cpp file).

"Ian Collins" <ia******@hotmail.comwrite
news:4i************@individual.net...
Madhav wrote:
hi all,
I was trying the following code on the MS .net compiler:

class Item {
static int count;

public:
//static void init() { }

Item()
{
count++;
cout<<"constructing Item"<<endl;
}
~Item()
{
count--;
cout<<"Destroying Item #"<<count<<endl;
}
};

The compiler kept giving me error for the unresloved symbol count.
After that , I made the variable count public and tried to assign 0 to
it in the main function. But still the same error was reported by the
compiler. I am confused about why the error has occured.
Do you have a definition of Item::count in a source file? Sounds like
you have forgotten to do this.

--
Ian Collins.

Jul 25 '06 #5
recover wrote:
Yes, you must write " int Item::count=0; " in the source file (normal in
the .cpp file).
Please don't top post!
"Ian Collins" <ia******@hotmail.comwrite
news:4i************@individual.net...
>>Madhav wrote:
>>>hi all,
I was trying the following code on the MS .net compiler:

class Item {
static int count;

public:
//static void init() { }

Item()
{
count++;
cout<<"constructing Item"<<endl;
}
~Item()
{
count--;
cout<<"Destroying Item #"<<count<<endl;
}
};

The compiler kept giving me error for the unresloved symbol count.
After that , I made the variable count public and tried to assign 0 to
it in the main function. But still the same error was reported by the
compiler. I am confused about why the error has occured.

Do you have a definition of Item::count in a source file? Sounds like
you have forgotten to do this.

--
Ian Collins.



--
Ian Collins.
Jul 25 '06 #6
recover wrote:
Yes, you must write " int Item::count=0; " in the source file (normal
in
the .cpp file).
Please don't top post!
Thanks. I will be careful of.
I use Outlook Express to reply news. It's template take me to wite at begin
of letter.
Jul 25 '06 #7

Ian Collins wrote:
luckyboy wrote:
try to make initialization in this manner in your constructor:
Item() :count(0)

No, this is wrong.

If you look back to the code you snipped, count is a static member. It
requires a definition.
Thanx Ian Collins., this really helped me, but i wanna mention that i
still beginner in C++ field and just i wanna help others to solve their
problems. Again Thank you :)

Jul 25 '06 #8
Ian Collins wrote:
luckyboy wrote:
try to make initialization in this manner in your constructor:
Item() :count(0)

No, this is wrong.

If you look back to the code you snipped, count is a static member. It
requires a definition.

--
Ian Collins.
hey Ian,
I wrote this in the main function by making the count
variable public:

int Item::count=0;

But now the compiler gives me something like:

error C2655: 'Item::count' : definition or redeclaration illegal in
current scope
see declaration of 'Item::count'

Then I moved the declaration above the main function and
everything worked fine. Can you please explain why it did not work in
previous case and works in this case?

I am not sure if the static variables follow the rules of
global variables for initialization. But I think this may just be it.

Regards,
Madhav.

Jul 25 '06 #9
Madhav wrote:

Please try and keep some context, you should have replied to my first
posting.
>
hey Ian,
I wrote this in the main function by making the count
variable public:

int Item::count=0;
There's your mistake, the definition should be in the global scope (or
the same namespace as the class), not in main.
But now the compiler gives me something like:

error C2655: 'Item::count' : definition or redeclaration illegal in
current scope
see declaration of 'Item::count'
It's telling you you can't put the definition in main.
Then I moved the declaration above the main function and
everything worked fine. Can you please explain why it did not work in
previous case and works in this case?
I think I just have!

--
Ian Collins.
Jul 25 '06 #10
Ian Collins wrote:
>
Then I moved the declaration above the main function and
everything worked fine. Can you please explain why it did not work in
previous case and works in this case?
I think I just have!
hm...but since count is a private variable, why I don't get error for
trying to access a private class member outside of the class? or this
case is an exception?

Jul 25 '06 #11
Madhav wrote:
Ian Collins wrote:
Then I moved the declaration above the main function and
everything worked fine. Can you please explain why it did not work in
previous case and works in this case?
>
I think I just have!
hm...but since count is a private variable, why I don't get error for
trying to access a private class member outside of the class? or this
case is an exception?
It's still private, but with static members, you must *declare* them in
the class but *define* them outside. Similarly, you might have a
private member function that is declared in the class but defined
outside of it:

class A
{
void DoSomething();
// ...
};

void A::DoSomething() { /*...*/ }

Cheers! --M

Jul 25 '06 #12
mlimber wrote:
It's still private, but with static members, you must *declare* them in
the class but *define* them outside. Similarly, you might have a
private member function that is declared in the class but defined
outside of it:

class A
{
void DoSomething();
// ...
};

void A::DoSomething() { /*...*/ }

Cheers! --M
Yes...hmm...thanks a lot Mr. X!

Jul 25 '06 #13
recover <zh**********@lingtu.comwrote:
>Please don't top post!

Thanks. I will be careful of.
I use Outlook Express to reply news. It's template take me to wite at begin
of letter.
Google for [outlook express quote fix].

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Jul 25 '06 #14

"Madhav" <ma***********@gmail.comwrote in message
news:11**********************@75g2000cwc.googlegro ups.com...
Ian Collins wrote:
>>
Then I moved the declaration above the main function and
everything worked fine. Can you please explain why it did not work in
previous case and works in this case?
I think I just have!
hm...but since count is a private variable, why I don't get error for
trying to access a private class member outside of the class? or this
case is an exception?
Because you're _not_ trying to access it outside the class. You're writing
a definition, which is different from an assignment statement.

An assignment, if it had worked for this, _would_ have gone in main or some
other function. A definition of a static member has to go _outside_ of any
function. I know it looks like an assignment, but it's not. It's an
initialization of a static member, which is called a "definition" of that
member.

-Howard
Jul 25 '06 #15

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

Similar topics

34
by: Andy | last post by:
1) Is there any use of defining a class with a single constructor declared in private scope? I am not asking a about private copy constructors to always force pass/return by reference. 2) Is...
2
by: Neno | last post by:
Hi, I have a linkage error that has something to do with the use of a static member array and I can't understand how to solve the problem. Can someone help me please? In detail: A class Month...
4
by: baumann | last post by:
hi all, according the private / protected access control, - private; that is, its name can be used only by members and friends of the class in which it is declared. - protected; that is,...
3
by: Diebels | last post by:
Hi, I have some problems using static variables which results in a core dump. I have attached code and coredump to the end of my message. I am trying to implement a kind of factory design. I...
12
by: jimmij | last post by:
Hi, Please look at the code bellow /*******************/ class ctab { private: static const unsigned n=48;
5
by: Joe Van Dyk | last post by:
class Foo { public: Foo() : smart_ptr_() { // reads some data int param = read_some_data(); // based off this data, we can create a Something object smart_ptr_ = smart_pointer<Something>(new...
10
by: sunil | last post by:
Hello, I am new to c# . I have some basic programming doubts. Please help me in clarifying these doubts. I want to initialize a static and readonly field with a value returned by a static...
8
by: John | last post by:
Hello, is there any compiler option for g++ for initializing static members of the class. Due to some unknown reason, static member in one of our c++ application is not getting initialized...
6
by: Grey Alien | last post by:
class A { public: A(const B& ref); private: static B& b ; }; How may b be initialized ?
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.