我正在尝试编译类似于以下代码段的代码:
class System
{
private:
struct Configuration
{
Configuration(/*params*/);
Configuration(const Configuration&);
Configuration& operator=(const Configuration&);
~Configuration();
/* member variables */
} m_config;
explicit System(const Configuration& cfg);
// Non copyable constructable, non assignable
System(const System&);
System& operator= (const System&);
public:
System();
~System();
}
//Implementation
System::System()
{
m_config = Configuration(/*default params*/);
// ....
}
Run Code Online (Sandbox Code Playgroud)
编译器错误:没有匹配函数来调用'System :: Configuration :: Configuration()'
当我提供(甚至只是一个声明而不是定义)嵌套结构的默认构造函数时,错误消失了 - 为什么?!
其他详细信息:gcc版本4.4.3(Ubuntu 4.4.3-4ubuntu5)
首先默认构造m_config然后分配.使用成员列表直接设置其值.
System::System()
: m_config(/*default params*/)
{}
Run Code Online (Sandbox Code Playgroud)