Declaration of a 'static' member data

Discussion in 'C++' started by vib.cpp@gmail.com, Feb 22, 2009.

  1. Guest

    #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;

    }
     
    , Feb 22, 2009
    #1
    1. Advertising

  2. * :
    > #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
     
    Alf P. Steinbach, Feb 22, 2009
    #2
    1. Advertising

  3. * Alf P. Steinbach:
    > * :
    >> #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
     
    Alf P. Steinbach, Feb 22, 2009
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    4
    Views:
    1,105
    Richard Tobin
    Dec 12, 2006
  2. dolphin
    Replies:
    3
    Views:
    1,366
    Pete Becker
    Dec 5, 2007
  3. Jeffrey
    Replies:
    7
    Views:
    427
    James Kanze
    Sep 30, 2008
  4. Michael Doubez
    Replies:
    11
    Views:
    1,005
    James Kanze
    Mar 26, 2011
  5. Skybuck Flying
    Replies:
    2
    Views:
    354
    Alf P. Steinbach /Usenet
    Jun 21, 2011
Loading...

Share This Page