By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,651 Members | 1,684 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,651 IT Pros & Developers. It's quick & easy.

Something with static...

P: n/a
Why is this struct illegal:

#include<iostream>

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
Share this Question
Share on Google+
5 Replies


P: n/a
desktop wrote:
Why is this struct illegal:

#include<iostream>

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

P: n/a

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

#include<iostream>

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

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

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

[SNIP]
Sep 25 '07 #3

P: n/a
On Tue, 25 Sep 2007 09:10:35 +0200, desktop wrote:
Why is this struct illegal:

#include<iostream>

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

P: n/a
Joel Yliluoma wrote:
On Tue, 25 Sep 2007 09:10:35 +0200, desktop wrote:
>Why is this struct illegal:

#include<iostream>

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

P: n/a
On Sep 25, 9:10 am, desktop <f...@sss.comwrote:
Why is this struct illegal:
#include<iostream>
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 objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Sep 26 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.