Declaring a private field in class

Discussion in 'C++' started by Johs, May 1, 2007.

  1. Johs

    Johs Guest

    I have this in a file called bas.h:

    #ifndef BAS_H_
    #define BAS_H_

    class GeoObj {
    public:
    void draw() const;
    int getNum();

    private:
    int num = 10;

    };


    #endif /*BAS_H_*/

    But I get the error:

    bas.h:10: error: ISO C++ forbids initialization of member ‘num’


    I have also tried to change "int num = 10;" to "static int num = 10";
    but it still gives an error. Why does it give an error to initialize a
    private field?
     
    Johs, May 1, 2007
    #1
    1. Advertisements

  2. Johs

    Ian Collins Guest

    Johs wrote:
    > I have this in a file called bas.h:
    >
    > #ifndef BAS_H_
    > #define BAS_H_
    >
    > class GeoObj {
    > public:
    > void draw() const;
    > int getNum();
    >
    > private:
    > int num = 10;


    Has to be "static const int = 10;"

    --
    Ian Collins.
     
    Ian Collins, May 1, 2007
    #2
    1. Advertisements

  3. Johs

    Rolf Magnus Guest

    Johs wrote:

    > I have this in a file called bas.h:
    >
    > #ifndef BAS_H_
    > #define BAS_H_
    >
    > class GeoObj {
    > public:
    > void draw() const;
    > int getNum();
    >
    > private:
    > int num = 10;
    >
    > };
    >
    >
    > #endif /*BAS_H_*/
    >
    > But I get the error:
    >
    > bas.h:10: error: ISO C++ forbids initialization of member ‘num’
    >
    >
    > I have also tried to change "int num = 10;" to "static int num = 10";
    > but it still gives an error. Why does it give an error to initialize a
    > private field?


    Because C++ doesn't allow it. You can't initialize member variables (except
    static constant integers) like that. Non-static member variables can only
    be initialized in the initializer list of the constructor.
     
    Rolf Magnus, May 1, 2007
    #3
  4. Johs

    Salt_Peter Guest

    On May 1, 4:28 am, Johs <> wrote:
    > I have this in a file called bas.h:
    >
    > #ifndef BAS_H_
    > #define BAS_H_
    >
    > class GeoObj {
    > public:
    > void draw() const;
    > int getNum();
    >
    > private:
    > int num = 10;
    >
    > };
    >
    > #endif /*BAS_H_*/
    >
    > But I get the error:
    >
    > bas.h:10: error: ISO C++ forbids initialization of member 'num'
    >
    > I have also tried to change "int num = 10;" to "static int num = 10";
    > but it still gives an error. Why does it give an error to initialize a
    > private field?


    Because you are attempting to initialize it in a formal declaration.
    What you attempting to do is the ctor's job.

    // bas.h
    #ifndef BAS_H_
    #define BAS_H_

    class GeoObj {
    int num;
    public:
    GeoObj(); // ctor
    void draw() const;
    int getNum() const;
    };

    #endif /*BAS_H_*/

    // bas.cpp
    #include "bas.h"

    // ctor with init list
    GeoObj::GeoObj() : num(10)
    {
    }

    void GeoObj::draw() const
    {
    // do stuff
    }

    int GeoObj::getNum() const
    {
    return num;
    }

    You can also provide an additional parametized ctor to set num.
    Or use one default ctor like so...

    GeoObj::GeoObj( int n = 10 ) : num( n )
    {
    }

    Don't ignore constructors in C++, including your copy constructor.
     
    Salt_Peter, May 1, 2007
    #4
  5. Johs

    Jim Langston Guest

    "Johs" <> wrote in message
    news:f16tne$ub6$-c.dk...
    >I have this in a file called bas.h:
    >
    > #ifndef BAS_H_
    > #define BAS_H_
    >
    > class GeoObj {
    > public:
    > void draw() const;
    > int getNum();
    >
    > private:
    > int num = 10;
    >
    > };
    >
    >
    > #endif /*BAS_H_*/
    >
    > But I get the error:
    >
    > bas.h:10: error: ISO C++ forbids initialization of member ‘num’
    >
    >
    > I have also tried to change "int num = 10;" to "static int num = 10"; but
    > it still gives an error. Why does it give an error to initialize a private
    > field?


    #ifndef BAS_H_
    #define BAS_H_

    class GeoObj {
    public:
    GeoObj(): num(10) {};
    void draw() const;
    int getNum();
    private:
    int num;
    };

    #endif /*BAS_H_*/
     
    Jim Langston, May 2, 2007
    #5
    1. Advertisements

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. Marc Twain
    Replies:
    2
    Views:
    1,048
    Marc Twain
    Nov 14, 2003
  2. DaveLessnau
    Replies:
    3
    Views:
    519
    Howard
    May 16, 2005
  3. Neroku
    Replies:
    6
    Views:
    10,682
    Chris Uppal
    Feb 8, 2007
  4. Hicham Mouline
    Replies:
    1
    Views:
    411
    Vladyslav Lazarenko
    Mar 27, 2009
  5. Sound
    Replies:
    2
    Views:
    598
    Randy Webb
    Sep 28, 2006
Loading...

Share This Page