调用struct类构造函数
Call constructor for struct class
namespace CommunicatorApi
{
class ApiObserver;
class COMM_API_EXPORT Api
{
public:
//! Basic constructor
Api(ApiObserver& observer);
//! Destructs the object and frees resources allocated by it
~Api();
}
}
我试图致电
#include <iostream>
#include "include/communicator_api.h"
using namespace std;
int main()
{
cout << "Hello, world, from Visual C++!" << endl;
CommunicatorApi::Api::Api();
}
但是我正在收到错误
CommunicatorApi::Api::Api no approprate default constructor available
您已经定义了具有参数的构造函数,因此未生成默认构造函数。自动生成默认/复制/移动CTOR和复制/移动分配操作员的条件?
,因为您的形式具有自定义定义的构造函数:
Api(ApiObserver& observer);
除非您也明确定义它,否则您不得使用默认构造函数。
您可以使用以下方法之一来解决问题。
选项1:定义默认构造函数
class COMM_API_EXPORT Api
{
public:
//! Default constructor
Api();
//! Basic constructor
Api(ApiObserver& observer);
//! Destructs the object and frees resources allocated by it
~Api();
}
然后,您可以使用:
CommunicatorApi::Api::Api();
选项2:使用自定义构造函数
CommunicatorApi::ApiObserver observer;
CommunicatorApi::Api::Api(observer);
ps
CommunicatorApi::Api::Api(observer);
创建一个临时对象。您可能想拥有以后可以使用的对象。为此,您需要:
CommunicatorApi::Api apiObject(observer);
相关文章:
- "error: no matching function for call to"构造函数错误
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 为什么在C++中使用私有复制构造函数与删除复制构造函数
- 选择要调用的构造函数
- 如何委托派生类使用其父构造函数?
- 构造函数正在调用一个使用当前类类型的函数
- 没有用于初始化C++中的变量模板的匹配构造函数
- 如何在c++中定义以struct为数据成员的类中的构造函数
- 用struct初始化C 向量时的构造函数误差
- 调用struct类构造函数
- 构造函数不允许在匿名聚合中,struct中的字符串
- 如何使用struct构造函数在结构中初始化指针阵列
- 使用struct插入std::map失败,出现构造函数错误
- 类中使用Struct以及构造函数和标头时出现C++正向声明错误
- "struct atomic"的复制构造函数
- 在c++中调用struct的构造函数时出现未定义的引用链接错误
- 是struct的默认构造函数,就像c++中的类一样
- 如何在c++类的构造函数中给出struct的向量作为参数?