我在初始化嵌套类构造函数时遇到问题.
这是我的代码:
#include <iostream>
using namespace std;
class a
{
public:
class b
{
public:
b(char str[45])
{
cout<<str;
}
}title;
}document;
int main()
{
document.title("Hello World"); //Error in this line
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我得到的错误是:
Run Code Online (Sandbox Code Playgroud)fun.cpp:21:30: error: no match for call to '(a::b)'
你可能想要这样的东西:
class a
{
public:
a():title(b("")) {}
//....
};
Run Code Online (Sandbox Code Playgroud)
这是因为title已经是它的成员a,但是你没有默认的构造函数.编写默认构造函数或在初始化列表中初始化它.