473,729 Members | 2,328 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Something with static...

Why is this struct illegal:

#include<iostre am>

struct debug {
std::string d1 = "bob\n";
};

I get this error:

graphics/debug.h:4: error: ISO C++ forbids initialization of member ‘d1’
graphics/debug.h:4: error: making ‘d1’ static
graphics/debug.h:4: error: invalid in-class initialization of static
data member of non-integral type ‘std::string’

If I change it to:

static std::string d1 = "bob\n";

I only get:

graphics/debug.h:4: error: invalid in-class initialization of static
data member of non-integral type ‘std::string’

I have also tried:

struct debug {
std::string d1 = getd1();
std::string getd1(){
return "bob\n";
}
};

But then I get:

graphics/debug.h:4: error: a function call cannot appear in a
constant-expression
I thought it was ok to call a function in a struct.
Sep 25 '07 #1
5 5616
desktop wrote:
Why is this struct illegal:

#include<iostre am>

struct debug {
std::string d1 = "bob\n";
};
Because the language says it is.

You can only define static const integral types in class declarations.
If I change it to:

static std::string d1 = "bob\n";

I only get:

graphics/debug.h:4: error: invalid in-class initialization of static
data member of non-integral type ‘std::string’
You have to declare the static member in the class declaration and
define it in a single compilation unit.

--
Ian Collins.
Sep 25 '07 #2

"desktop" <ff*@sss.comwro te in message
news:fd******** **@news.net.uni-c.dk...
Why is this struct illegal:

#include<iostre am>

struct debug {
std::string d1 = "bob\n";
};
Try this.

struct debug {
std::string d1;
debug(): d1( "bob\n" ){}
}

That's called an "initializa iton list". But it is no longer a POD (plain
old data).

[SNIP]
Sep 25 '07 #3
On Tue, 25 Sep 2007 09:10:35 +0200, desktop wrote:
Why is this struct illegal:

#include<iostre am>

struct debug {
std::string d1 = "bob\n";
};
I get this error:
graphics/debug.h:4: error: ISO C++ forbids initialization of member ‘d1’
graphics/debug.h:4: error: making ‘d1’ static
graphics/debug.h:4: error: invalid in-class initialization of static
data member of non-integral type ‘std::string’
In-class initialization of data members is only allowed for
static const POD types.

For example:
struct debug
{
static const int = 5;
};

To initialize your code, you need to use a constructor:

struct debug
{
std::string d1;

debug() : d1("bob\n") { }
};

That's where a function call also is okay:

struct debug
{
std::string d1;

debug() : d1(getd1()) { }

const std::string getd1() const { return "bob\n"; }
};

--
Joel Yliluoma - http://bisqwit.iki.fi/
: comprehension = 1 / (2 ^ precision)
Sep 25 '07 #4
Joel Yliluoma wrote:
On Tue, 25 Sep 2007 09:10:35 +0200, desktop wrote:
>Why is this struct illegal:

#include<iostr eam>

struct debug {
std::string d1 = "bob\n";
};
I get this error:
graphics/debug.h:4: error: ISO C++ forbids initialization of member ‘d1’
graphics/debug.h:4: error: making ‘d1’ static
graphics/debug.h:4: error: invalid in-class initialization of static
data member of non-integral type ‘std::string’

In-class initialization of data members is only allowed for
static const POD types.
No, only integral types. You can't write

static const float f = 42.0;

for example.

--
Ian Collins.
Sep 25 '07 #5
On Sep 25, 9:10 am, desktop <f...@sss.comwr ote:
Why is this struct illegal:
#include<iostre am>
struct debug {
std::string d1 = "bob\n";
};
And what is it supposed to mean? There's a d1 for every
instance of the class; if you want to initialize it, you do so
in the constructor.
I get this error:
graphics/debug.h:4: error: ISO C++ forbids initialization of member ?d1?
graphics/debug.h:4: error: making ?d1? static
graphics/debug.h:4: error: invalid in-class initialization of static
data member of non-integral type ?std::string?
If I change it to:
static std::string d1 = "bob\n";
I only get:
graphics/debug.h:4: error: invalid in-class initialization of static
data member of non-integral type ?std::string?
The declaration of a static member in a class is just that; a
declaration, and not a definition. (There are technical
reasons, largely historic, for this, and I don't see it changing
any time soon.) Since it's only a declaration: 1) you need to
provide a definition somewhere, and 2) you can't initialize it.
(The initialization belongs in the definition.)

There is a special exception to this rule for static members
with a const integral type, and which is initialized with a
constant integral expression; in this case (and only in this
case), the initialization can be provided in the declaration in
the class body (but you still need to provide a definition).
I have also tried:
struct debug {
std::string d1 = getd1();
std::string getd1(){
return "bob\n";
}
};
But then I get:
graphics/debug.h:4: error: a function call cannot appear in a
constant-expression
I thought it was ok to call a function in a struct.
You can *define* a function in a class, and you can call
anything from a that function, but a class defines a data type,
and is not executable code.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Sep 26 '07 #6

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

Similar topics

6
3468
by: Maria Gaitani | last post by:
Hi! I am trying to programme some java native interface but I'm still in the process of research. I've seen examples such as this one http://java.sun.com/docs/books/tutorial/native1.1/stepbystep/step1.html but I don't understand the third and fourth line of the code of the above example:
13
2208
by: Ole Olsen | last post by:
I originally come from a VB6 background, now entirely programming in C#. All in all a great language (C#) with a lot of powerful entities; there is however one thing that I just can't seem to figure out. In VB6, when creating a Class, you can mark it at "PublicNotCreatable", meaning that it is not possible to instantiate it directly. This would one normally do, it you want to force people to access this concrete class through a defined...
3
1573
by: maadhuu | last post by:
hello I have a problem and it is something like this . I have 5 static arrays in one class called S1 ,S2 ....S5(all 2 D arrays). Now , I want to access some number of elements in each one of the arrays . But i don't want to explicitly specify the names of all the arrays , say S1 or S2 in order to do a similar look up . What are the design ways in which I can get this done. ?? One possibility was that I could call a function with an...
5
1936
by: Daniel Vukadinovic | last post by:
Can anyone explain me these things in C++? 1.What is :: used for like in the next case: if(.... { ... ::one;
59
5120
by: MotoK | last post by:
Hi Experts, I've just joined this group and want to know something: Is there something similar to smart pointers in C or something to prevent memory leakages in C programs. Regards MotoK
5
3805
by: admin | last post by:
ok This is my main. Pretty much it goes through each category and starts up 4 worker threads that then ask for groups to gether from. My problem is that when the thread gets done it keeps the mysql connections open so I end up with quite a few at the end. Is there a different way that I should do this? class Program { static string categories = { "emulation" , "audio" , "console" , "anime" , "xxx" , "tv" , "pictures" , "video" };
11
1858
by: Matthew | last post by:
Ok let me try to explain this as good as I can. I am creating this application where it contains a userlogin class. The user logs in before entering the main apploication. I want to do audit trails and I am trying to figure out how to get the user ID that logged in. Can anyone help me. I have samples if anyone needs them.
2
2963
by: patrickdepinguin | last post by:
Hi, I use zlib to write data structures to a compressed file, using the gzwrite function. Afterwards I read the data back with gzread. I notice that this works well when the data written is not that much, but when there is more data to write, after a while I get data errors when reading back the data. Error in main: couldn't read stat zlib error -3: test512-20070531-18h10m02.stat.gz: data error
5
6387
by: hnshashi | last post by:
I have writtem kernel(2.4) module to commu. with user space appl. using netlink socket. I am getting compilation error. kernel module:-> #include <linux/skbuff.h> #include<linux/module.h> #include <linux/socket.h> #include <linux/config.h> #include <linux/module.h> #include <linux/kernel.h>
0
8921
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9284
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9202
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
8151
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...
1
6722
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6022
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
4796
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3238
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
3
2165
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.