Nested class initialization list question
//header file
class Foo
{
public:
struct FooBar {
bool ok;
string msg;
};
};
// .c开发者_StackOverflow社区pp file
Foo::FooBar::FooBar():ok(false), msg("hello")
{}
When attempting to compile, I get:
error: definition of implicitly-declared ‘Foo::FooBar::FooBar()’
Silly, simple scope related mistake I'm sure, but I can't seem to spot what it is ...
You're trying to implement a constructor that you didn't declare. Declare it like this:
//header file
class Foo
{
public:
struct FooBar {
FooBar();
bool ok;
string msg;
};
};
// .cpp file
Foo::FooBar::FooBar():ok(false), msg("hello")
{}
You need to declare the constructor, i.e.
class Foo
{
public:
struct FooBar {
FooBar(); // <-- needed
bool ok;
string msg;
};
};
When you omit the declaration of a constructor for a class, an implicit constructor definition is created by the compiler. In this case, you have defined the default constructor, but did not declare it, so the compiler is confused as to what's going on. Thus, it is complaining about you defining an implicitly defined default constructor.
The solution is to declare the default constructor, so that it knows to use your default constructor definition.
精彩评论