#include <iostream>
using namespace std;
int main()
{
string str="abcd";
cout <<str <<endl; //error C2679: 二进制“<<”: 没有找到接受“std::string”类型的右操作数的运算符(或没有可接受的转换)
return 0;
}
当你写下这样的代码时你将会得到一大堆的编译错误, 其中比较有用的在
第一行: error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string'.
第二行:
没有与这些操作数匹配的 "<<" 运
算符
操作数类型为: std::ostream << std::string
。
意思是说, 没有能够执行 operator<<(const std::string &) 的函数.
, 那么问题出在了哪里?
记得 Thinking in C++ 一书中说过, 将不是属于类的行为作为普通或者友员函数实现. 对于本例而言, 对 string 类的输出不是 string 的行为, 也不是 cout(ostream 的一个对象) 的行为, 因为 cout 不光输出 string 类型, 它还会输出别的很多类型. 因此, 对于 string 的输出, 不应该作为 cout 的一个成员. 所以
cout <<str <<endl;
是无法通过编译的, 因为 iostream.h 中根本没有对 operator<<(string) 的重载.
根据 C++ Name lookup 约定, cout 函数会进行如下查找:
- 查找 cout 的命名空间中, 使用点之前是否有 operator<<(std::ostream &, const std::string &) 的声明.
显然, iostream 中并没有相关的函数声明, 然而真正对于 operator<<(std::ostream &, const std::string &) 的声明在头文件 string 中. 将 cout 对 string 的重载放在 string 这个头文件中是正确的设计. 这也是本文阐述的主旨.
因此, 让上述代码工作, 仅需要加入#include< string> 头文件.
#include<string>是string容器的头文件
#include<cstring>是c++对c语言头文件#include<string.h>的改写。你明白了吗?