为什么我不能cout一个string?
为什么我不能cout string : 
 string text ; text = WordList[i].substr(0,20) ; cout << "String is : " << text << endl ; 
当我这样做,我得到以下错误:
错误2错误C2679:二进制'<<':没有find操作符'std :: string'types的右边操作数(或没有可接受的转换)c:\ users \ mollasadra \ documents \ visual studio 2008 \ projects \ barnamec \ barnamec \ barnamec.cpp 67 barnamec **
令人惊讶的是,即使这不起作用:
 string text ; text = "hello" ; cout << "String is : " << text << endl ; 
	
你需要包括
 #include <string> #include <iostream> 
 你需要以某种方式引用cout的命名空间std 。 例如,插入 
 using std::cout; using std::endl; 
在你的函数定义之上,或者文件。
你的代码有几个问题:
-   WordList没有在任何地方定义。 你应该在使用之前定义它。
- 你不能在这样的函数之外编写代码。 你需要把它放在一个函数中。
-  在使用cout或者endl之前,你需要#include <string>然后才能使用string类和iostream。
-   string,cout和endl住在std命名空间中,所以如果不用std::前缀,就不能访问它们,除非你使用using指令将它们引入到范围中。
 您不必显式引用std::cout或std::endl 。 
 它们都包含在namespace std 。  using namespace std而不是使用范围parsing运算符::每一次使得更容易和更清洁。 
 #include<iostream> #include<string> using namespace std; 
如果你使用的是linux系统,那么你需要添加
 using namespace std; 
标题下方
 如果窗口,然后确保你正确地把头文件#include<iostream.h> 
 #include<string.h> 
请参阅它完美的工作。
 #include <iostream> #include <string> int main () { std::string str="We think in generalities, but we live in details."; // (quoting Alfred N. Whitehead) std::string str2 = str.substr (3,5); // "think" std::size_t pos = str.find("live"); // position of "live" in str std::string str3 = str.substr (pos); // get from "live" to the end std::cout << str2 << ' ' << str3 << '\n'; return 0; }