Something with static...

D

desktop

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.
 
J

Jim Langston

desktop said:
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]
 
I

Ian Collins

desktop said:
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.
 
J

Joel Yliluoma

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"; }
};
 
I

Ian Collins

Joel said:
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.
 
J

James Kanze

Why is this struct illegal:

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,201
Messages
2,571,048
Members
47,647
Latest member
NelleMacy9

Latest Threads

Top