如何在C++中初始化嵌套类的构造函数

san*_*box 4 c++ constructor

我在初始化嵌套类构造函数时遇到问题.

这是我的代码:

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

我得到的错误是:

fun.cpp:21:30: error: no match for call to '(a::b)'
Run Code Online (Sandbox Code Playgroud)

Luc*_*ore 6

你可能想要这样的东西:

class a
{
public:
    a():title(b("")) {}
    //....
};
Run Code Online (Sandbox Code Playgroud)

这是因为title已经是它的成员a,但是你没有默认的构造函数.编写默认构造函数或在初始化列表中初始化它.