Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Declaration of a 'static' member data

Reply
Thread Tools

Declaration of a 'static' member data

 
 
vib.cpp@gmail.com
Guest
Posts: n/a
 
      02-22-2009
#include <iostream.h>

class Cat
{
public:
static HowManyCats;
private:
int itsage;
};

int Cat::HowManyCats=5;

int main()
{
cout<<"HowManyCats="<<Cat::HowManyCats<<endl;
}

The above is right, but the below is wrong, why cannot I declare
static data member in the class which it belong to?


#include <iostream.h>

class Cat
{
public:
static int HowManyCats;
private:
int itsage;
};

Cat::HowManyCats=5;

int main()
{
cout<<"HowManyCats="<<Cat::HowManyCats<<endl;
}


The following is still not true, why? can I declare the static member
inside the main function? And if I remove the int from the int
Cat::HowManyCats=5;statement, it passed the compile, but still blocked
when building, can anybody explain this for me?

#include <iostream.h>

class Cat
{
public:
static HowManyCats;
private:
int itsage;
};


int main()
{
int Cat::HowManyCats=5;
cout<<"HowManyCats="<<Cat::HowManyCats<<endl;

}
 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      02-22-2009
* http://www.velocityreviews.com/forums/(E-Mail Removed):
> #include <iostream.h>


Note: this is a non-standard header. Some compilers lack this header. You should use

#include <iostream>

which is a standard header.


> class Cat
> {
> public:
> static HowManyCats;
> private:
> int itsage;
> };
>
> int Cat::HowManyCats=5;
>
> int main()
> {
> cout<<"HowManyCats="<<Cat::HowManyCats<<endl;
> }
>
> The above is right, but the below is wrong, why cannot I declare
> static data member in the class which it belong to?
>
>
> #include <iostream.h>
>
> class Cat
> {
> public:
> static int HowManyCats;
> private:
> int itsage;
> };
>
> Cat::HowManyCats=5;


This definition lacks a type.


> int main()
> {
> cout<<"HowManyCats="<<Cat::HowManyCats<<endl;
> }
>
>
> The following is still not true, why? can I declare the static member
> inside the main function? And if I remove the int from the int
> Cat::HowManyCats=5;statement, it passed the compile, but still blocked
> when building, can anybody explain this for me?
> #include <iostream.h>
>
> class Cat
> {
> public:
> static HowManyCats;
> private:
> int itsage;
> };
>
>
> int main()
> {
> int Cat::HowManyCats=5;
> cout<<"HowManyCats="<<Cat::HowManyCats<<endl;
>
> }


When you remove the 'int' in the code above you have an expression instead of a
declaration. An expression followed by a semicolon is a valid C++ statement. For
example,

42;

is a valid C++ statement where the value of the expression is discarded.

You should up your compiler's warning level so that it warns about statements
with no effect.

With g++ you can do that with '-Wall', with Visual C++ you can do that with '/W4'.

You get a link error because in this latest code there is no definition of
'Cat::HowManyCats'.

It has been declared but not defined.


Cheers & hth.,

- Alf
 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      02-22-2009
* Alf P. Steinbach:
> * (E-Mail Removed):
>> #include <iostream.h>

>
> Note: this is a non-standard header. Some compilers lack this header.
> You should use
>
> #include <iostream>
>
> which is a standard header.
>
>
>> class Cat
>> {
>> public:
>> static HowManyCats;
>> private:
>> int itsage;
>> };
>>
>> int Cat::HowManyCats=5;
>>
>> int main()
>> {
>> cout<<"HowManyCats="<<Cat::HowManyCats<<endl;
>> }
>>
>> The above is right, but the below is wrong, why cannot I declare
>> static data member in the class which it belong to?
>>
>>
>> #include <iostream.h>
>>
>> class Cat
>> {
>> public:
>> static int HowManyCats;
>> private:
>> int itsage;
>> };
>>
>> Cat::HowManyCats=5;

>
> This definition lacks a type.
>
>
>> int main()
>> {
>> cout<<"HowManyCats="<<Cat::HowManyCats<<endl;
>> }
>>
>>
>> The following is still not true, why? can I declare the static member
>> inside the main function? And if I remove the int from the int
>> Cat::HowManyCats=5;statement, it passed the compile, but still blocked
>> when building, can anybody explain this for me?
>> #include <iostream.h>
>>
>> class Cat
>> {
>> public:
>> static HowManyCats;
>> private:
>> int itsage;
>> };
>>
>>
>> int main()
>> {
>> int Cat::HowManyCats=5;
>> cout<<"HowManyCats="<<Cat::HowManyCats<<endl;
>>
>> }

>
> When you remove the 'int' in the code above you have an expression
> instead of a declaration. An expression followed by a semicolon is a
> valid C++ statement. For example,
>
> 42;
>
> is a valid C++ statement where the value of the expression is discarded.
>
> You should up your compiler's warning level so that it warns about
> statements with no effect.


Note: the particular statement resulting from removing 'int' above has an effect
(it is an assignment). I didn't see that before hitting the Send button. But you
should up your compiler's warning level anyway.


>
> With g++ you can do that with '-Wall', with Visual C++ you can do that
> with '/W4'.
>
> You get a link error because in this latest code there is no definition
> of 'Cat::HowManyCats'.
>
> It has been declared but not defined.
>
>
> Cheers & hth.,
>
> - Alf

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Class declaration: TMember Member(); vs TMember Member; Skybuck Flying C++ 2 06-21-2011 02:36 PM
Class Member Data and Member Function Parameters - Should Parameters Be Data Members? Jason C++ 2 05-13-2006 07:11 AM
Use of typeof() for member data declaration (in g++) M. Steinbach C++ 1 02-20-2004 10:28 AM
Use of typeof() for member data declaration (g++ bug?) M. Steinbach C++ 0 02-20-2004 08:40 AM
How would I use qsort to sort a struct with a char* member and a long member - I want to sort in order of the long member Angus Comber C Programming 7 02-05-2004 06:41 PM



Advertisments