如何初始化C ++中嵌套类的构造方法 [英] How to initialise Constructor of a Nested Class in C++
本文介绍了如何初始化C ++中嵌套类的构造方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在初始化嵌套类构造函数时遇到问题。
这是我的代码:
#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); //错误在这行
return 0;
}
我得到的错误是:
fun.cpp:21:30:错误:没有匹配到调用'(a :: b)'
解决方案
>
class a
{
public:
a():title(b()){ }
// ...
};
这是因为 title
的 a
,但是你没有一个默认的构造函数。要么编写默认构造函数,要么在初始化列表中初始化它。
I am getting problems in initializing the nested class constructor.
Here is my code:
#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;
}
The error I get is:
fun.cpp:21:30: error: no match for call to '(a::b)'
解决方案
You probably want something like:
class a
{
public:
a():title(b("")) {}
//....
};
This is because title
is already a member of a
, however you don't have a default constructor for it. Either write a default constructor or initialize it in the initialization list.
这篇关于如何初始化C ++中嵌套类的构造方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文