473,656 Members | 2,997 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2720
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.com 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.
May 1 '07 #4

"Johs" <as****@asd.com wrote 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
2395
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 relevant announcements. If you're interested, drop me a note. But if for some reason you think that this discussion is fine here at the c.l.py, please let me know. ** LONG POST AHEAD **
1
1864
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 pointers to objects type of foo by duplicating the objects themselves. So, I had to call the constructor of foo class which must take in the name to initialize the private name field. To get the name, I implemented a function called get_name()...
1
2147
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 definition. If you insted have declared a primitive type or a class type then I would understand it completely. I assume that having a friend declaration in the private section make no sense. I assume that you always put friend declaration in the...
6
1876
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. This example is less practical than some, but more illustrative than most. I came up with this to show a fellow programmer why I think he's using As New ... too much, and I though I might as well share it here, too. First create a class as...
3
6772
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 mymemberfield = new CodeMemberField(fieldType,fieldName);
7
9290
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 from my code. My stab at the problem is shown below. Can anyone suggest what my most efficient workable solution would be (i.e. I don't want to have to create instances of the classes as they will only store constant information and I would...
5
3143
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 same class. Is this correct? It would be nice to declare the MySelf instance in the Class public space (just under Private Class Class1). That will give this error: Cannot refer to an instance member of a class from within a shared
8
1902
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 the data as either a int or a Guid depending on the structure of the database. If we simply used int to represent a primary key, rather than this PrimaryKey class then programmers could do things like performing mathematically operations on the...
2
3335
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 class B has an instance of template class A, which has some base type T (usually int or double). However, the base type T is important to calculations in B, so I would like to obtain access to the type for further variable declaration within B. ...
0
8296
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8497
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8598
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7310
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5627
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4150
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2721
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1928
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1598
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.