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