473,322 Members | 1,806 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Declaring a private field in class

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?
May 1 '07 #1
4 2689
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.
May 1 '07 #2
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.

May 1 '07 #3
On May 1, 4:28 am, Johs <asd...@asd.comwrote:
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.
May 1 '07 #4

"Johs" <as****@asd.comwrote in message
news:f1**********@news.net.uni-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_*/
May 2 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Carlos Ribeiro | last post by:
Hello all, I'm posting this to the list with the intention to form a group of people interested in this type of solution. I'm not going to spam the list with it, unless for occasional and...
1
by: mehmet canayaz | last post by:
I have a class called foo and my class foo has a private field called "name". In my bar.cpp file i have #includes "foo.h" and in bar.cpp I also have a STATIC function that duplicates an array of...
1
by: Tony Johansson | last post by:
Hello experts! Assume I have this class definition of class ListElem. I'm a bit unsure how to interpret when you put friend declaration in public, protected and private section of a class...
6
by: Steve Jorgensen | last post by:
Many of the regulars here have explained that declaring variables using As New .... is a bad idea, and some have given some good explanations, but I wanted add one more demonstration to the mix. ...
3
by: | last post by:
Using CodeDOM, is there a way to declare and set a private field. Something like: private const int tableConstant = 10; //Provide the type and variable name CodeMemberField...
7
by: Iain Mcleod | last post by:
Hi This must be an often encountered problem. I want to declare an abstract class or an interface with nothing but several static constants so that I can use polymorphism when I call each of them...
5
by: Brett | last post by:
In a class, I have several Private subs. I declare an instance of the class such as: Dim MySelf as new Class1 within a private sub. The motive is to provide access to other subs within the...
8
by: Dave A | last post by:
I have a class called 'PrimaryKey' that represents the primary key of a table. PrimaryKeys can only be created and the class only implements .ToString(). The PrimaryKey class internally stores...
2
by: pagekb | last post by:
Hello, I'm having some difficulty compiling template classes as containers for other template objects. Specifically, I have a hierarchy of template classes that contain each other. Template...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.