Declaring a private field in class

J

Johs

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?
 
I

Ian Collins

Johs said:
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;"
 
R

Rolf Magnus

Johs said:
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.
 
S

Salt_Peter

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.
 
J

Jim Langston

Johs said:
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_*/
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top