// vs 2010
//

#inlcude "stdafx.h"
#include <iostream>
using namespace std;

int main(int argc, char *argv[])
{
  string str;
  cout <<str <<endl;      // Compilation error.

  return 0;
}
当你写下这样的代码时你将会得到一大堆的编译错误, 其中比较有用的在第一行:  error C2679: binary '<<' : no operator found which takes a right-hand operand of type '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 这个头文件中是正确的设计. 这也是本文阐述的主旨.
因此, 让上述代码工作, 仅需要加入 string 头文件.

相关文章:

  • 2021-05-26
  • 2021-08-29
  • 2021-05-21
  • 2022-02-05
  • 2021-09-27
  • 2021-07-19
  • 2021-11-27
猜你喜欢
  • 2022-01-26
  • 2021-11-21
  • 2021-06-20
  • 2022-12-23
  • 2022-12-23
  • 2021-11-04
  • 2021-06-13
相关资源
相似解决方案