如何在标题文件之外制作构造函数的定义
How to make the definition of the constructor outside the header file?
我在标题文件中具有此构造函数:
class Fan {
Id id;
string name;
Age age;
public:
Fan(Id id, string name, Age age);
};
和.cpp中的这个定义
Fan::Fan(Id id, string name="someone", Age age=0) : id(id), name(name),
age(age), status(disconnected)
{
if(id<0 || age<0) {
throw BadParams();
}
}
我有此笔记:( mtm是名称空间)
..Fan.h:60:2: note: mtm::Fan::Fan(mtm::Id, std::string, mtm::Age)
..Fan.h:60:2: note: candidate expects 3 arguments, 2 provided
..Fan.h:47:7: note: mtm::Fan::Fan(const mtm::Fan&)
..Fan.h:47:7: note: candidate expects 1 argument, 2 provided
我试图在声明和定义之前进行内联,并得到此错误:
..Fan.h:60:9: note: mtm::Fan::Fan(mtm::Id, std::string, mtm::Age)
..Fan.h:60:9: note: candidate expects 3 arguments, 2 provided
..Fan.h:47:7: note: mtm::Fan::Fan(const mtm::Fan&)
..Fan.h:47:7: note: candidate expects 1 argument, 2 provided
..Fan.h:60:9: warning: inline function 'mtm::Fan::Fan(mtm::Id, std::string, mtm::Age)' used but never defined [enabled by default]
您必须将默认参数放在声明中(即.h文件中),而不是.cpp文件中。
即:
class Fan {
public:
Fan(Id id, string name="someone", Age age=0);
};
相关文章:
- 具有enable_if外部类原型的模板类构造函数定义
- 构造函数定义中类声明中的模板值
- C++中的继承和构造函数定义
- 如何使用模板化构造函数定义演绎指南?
- 构造函数定义C
- 不正确的成员构造函数定义
- 具有虚拟多重继承的构造函数定义
- C++谷歌测试在哪里放置测试夹具构造函数定义
- 模板类定义中的模板构造函数定义
- C++下面给出的代码中的构造函数定义差异
- 带继承的构造函数定义
- 无法访问构造函数定义之外的变量
- 调用构造函数定义中的函数后收到Segmentation Fault 11错误
- 将具有默认值的构造函数定义为组合对象的私有字段
- 防止重新定义构造函数定义的方法
- 继承构造函数-定义可移植的固定大小类型(C++)
- 在类的私有部分为复制构造函数定义原型如何防止类的复制
- 如何指定继承类的构造函数定义
- 了解默认移动构造函数定义
- 理解构造函数定义块的语法变化