重载流操作符时遇到的问题(输出自定义数据类型到文件)
如下代码,将Test类通过重载流操作符输出到文件,编译时会报错(二义性)
| 1 | 
 | 

错误原因
- std内置函数 - 1 - std::ostream& operator<<(std::ostream&, const int); 
- 自定义函数 - 1 - ofstream& operator<<(ofstream& ofs, const Test& test); 
- 当运行到 - 1 - ofs << test.num; 
- << 运算符的两个参数类型分别为(ofstream,int) 
- 对于内置函数,第一个参数为父类引用指向子类对象(ostream -> ofstream),第二个参数完全匹配。 
- 对于自定义函数,第一个参数完全匹配,第二个参数因为Test类的构造函数可以隐式调用。Test test = 1; 是合法的 
- 编译器不知道调用哪个函数来处理这个语句 
解决办法
- 将构造函数前加explicit关键字,使构造函数不可进行隐式转换 - 1 
 2
 3
 4
 5
 6
 7
 8- class Test 
 {
 public:
 explicit Test(int num) {
 this->num = num;
 }
 int num;
 };
- 将 << 的参数改为 ostream,重载ostream即可 - 1 
 2
 3
 4- ostream& operator<<(ostream& ofs, const Test& test) { 
 ofs << test.num;
 return ofs;
 }
 评论



