operator << 重载 注意事项
2013-06-07 20:31
134 查看
进行类的运算操作符重载时,需要把涉及到的运算符重载的类的头文件包含近年来,例如:
#include<string>
如果没有包含上面头文件,则下面的类进行operator <<重载时会出现编译错误:binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)
class A{
friend ostream& operator <<(ostream& out, A& a);
private:
string str;
}
std::ostream& operator <<(std::ostream& out, A& a)
{
out <<a.str<<endl;
}
这是因为在<string>中定义了string的"<<"重载运算符,但是没有包含头文件,所以在A中的str输出是编译器无法解析的。
#include<string>
如果没有包含上面头文件,则下面的类进行operator <<重载时会出现编译错误:binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)
class A{
friend ostream& operator <<(ostream& out, A& a);
private:
string str;
}
std::ostream& operator <<(std::ostream& out, A& a)
{
out <<a.str<<endl;
}
这是因为在<string>中定义了string的"<<"重载运算符,但是没有包含头文件,所以在A中的str输出是编译器无法解析的。
相关文章推荐
- ostream & operator 重载操作流的注意事项
- 操作符(运算符)重载注意事项(含模板类中<<、>>重载)
- 以模板的方式重载"operator <<"需要注意的地方
- C++_重载运算符<< ostream流重载注意事项_使自定义数据可以用cout输出
- 在centos系统shell脚本中cat和重定向符号<<EOF结合使用的注意事项
- 赋值运算符重载(operator=)注意事项
- SoapUI使用注意事项——在<!CDATA>节点之前不要有空格
- 自定义Camera综述(一般步骤、注意事项、遇到的难题<【内存溢出问题】>、像素参考)
- C++ operator= 注意事项
- 程序员之---C语言细节22(函数返回指针注意事项<悬空指针>、查看进程能够分配的内存大小)
- 程序猿之---C语言细节22(函数返回指针注意事项<悬空指针>、查看进程可以分配的内存大小)
- 移动web注意事项 <<转>>
- <s:param>注意事项
- 利用operator关键字在对象间进行赋值的几点注意事项(1)
- C++默认参数与函数重载 注意事项
- C++默认参数与函数重载 注意事项
- mybatis 传参是 list<string> 的注意事项
- mybatis映射文件中<if>使用注意事项
- 工作小结:Base64注意事项、<label>标签for属性
- 游戏后端主程工作内容及游戏项目中的注意事项及游戏项目中注意事项<转载>