为什么我不cout
string
喜欢这个:
string text ;
text = WordList[i].substr(0,20) ;
cout << "String is : " << text << endl ;
Run Code Online (Sandbox Code Playgroud)
当我这样做时,我收到以下错误:
错误2错误C2679:二进制'<<':找不到带有'std :: string'类型的右手操作数的运算符(或者没有可接受的转换)c:\ users\mollasadra\documents\visual studio 2008\projects\barnamec\barnamec\barnamec.cpp 67 barnamec**
令人惊讶的是,即使这不起作用:
string text ;
text = "hello" ;
cout << "String is : " << text << endl ;
Run Code Online (Sandbox Code Playgroud)
Kir*_*rov 229
你需要包括
#include <string>
#include <iostream>
Run Code Online (Sandbox Code Playgroud)
unp*_*680 11
你需要以std
某种方式引用cout的命名空间.例如,插入
using std::cout;
using std::endl;
Run Code Online (Sandbox Code Playgroud)
在函数定义或文件之上.
您的代码有几个问题:
WordList
没有在任何地方定义.您应该在使用它之前定义它.#include <string>
在使用cout
或之前,您需要先使用字符串类和iostream endl
.string
,cout
并endl
住在std
命名空间,所以你不能没有用前缀访问它们std::
,除非你使用的using
指令,以使它们的范围第一次.