468,491 Members | 2,072 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

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 5612
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

34 posts views Thread by Andy | last post: by
3 posts views Thread by Diebels | last post: by
12 posts views Thread by jimmij | last post: by
5 posts views Thread by Joe Van Dyk | last post: by
10 posts views Thread by sunil | last post: by
8 posts views Thread by John | last post: by
reply views Thread by NPC403 | last post: by
3 posts views Thread by gieforce | last post: by
reply views Thread by theflame83 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.