Connecting Tech Pros Worldwide Forums | Help | Site Map

Something with static...

desktop
Guest
 
Posts: n/a
#1: Sep 25 '07
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.

Ian Collins
Guest
 
Posts: n/a
#2: Sep 25 '07

re: Something with static...


desktop wrote:
Quote:
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.
Quote:
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.
Jim Langston
Guest
 
Posts: n/a
#3: Sep 25 '07

re: Something with static...



"desktop" <fff@sss.comwrote in message
news:fdac8b$c5o$1@news.net.uni-c.dk...
Quote:
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]


Joel Yliluoma
Guest
 
Posts: n/a
#4: Sep 25 '07

re: Something with static...


On Tue, 25 Sep 2007 09:10:35 +0200, desktop wrote:
Quote:
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)
Ian Collins
Guest
 
Posts: n/a
#5: Sep 25 '07

re: Something with static...


Joel Yliluoma wrote:
Quote:
On Tue, 25 Sep 2007 09:10:35 +0200, desktop wrote:
Quote:
>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.
James Kanze
Guest
 
Posts: n/a
#6: Sep 26 '07

re: Something with static...


On Sep 25, 9:10 am, desktop <f...@sss.comwrote:
Quote:
Why is this struct illegal:
Quote:
#include<iostream>
Quote:
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.
Quote:
I get this error:
Quote:
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?
Quote:
If I change it to:
Quote:
static std::string d1 = "bob\n";
Quote:
I only get:
Quote:
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).
Quote:
I have also tried:
Quote:
struct debug {
std::string d1 = getd1();
std::string getd1(){
return "bob\n";
}
};
Quote:
But then I get:
Quote:
graphics/debug.h:4: error: a function call cannot appear in a
constant-expression
Quote:
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:james.kanze@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

Closed Thread