为什么我不能发出字符串?

| 为什么我不能这样
cout
:1ѭ:
string text ;
text = WordList[i].substr(0,20) ;
cout << \"String is  : \" << text << endl ;
当我这样做时,出现以下错误:   错误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 ;
    
已邀请:
        您需要包括
#include <string>
#include <iostream>
    
        您需要以某种方式引用cout \的命名空间“ 5”。例如,插入
using std::cout;
using std::endl;
在函数定义或文件之上。     
        您的代码有几个问题: anywhere7ѭ在任何地方都没有定义。您应该先定义它,然后再使用它。 您不能只在这样的函数之外编写代码。您需要将其放入函数中。 在使用字符串类和iostream之前,必须先使用
#include <string>
,然后再使用
cout
endl
string
cout
endl
驻留在
std
名称空间中,因此,除非您先使用the16 scope指令将它们带入作用域,否则您必须在未使用
std::
前缀的情况下访问它们。     
        上面的答案很好,但是如果不想添加字符串包含,则可以使用以下内容
ostream& operator<<(ostream& os, string& msg)
{
os<<msg.c_str();

return os;
}
    
        您不必明确引用ѭ18或
std::endl
。 它们都包含在ѭ20中。每次使用
using namespace std
而不是使用示波器分辨率运算符
::
都变得更加轻松和整洁。
#include<iostream>
#include<string>
using namespace std;
    
        如果您使用的是Linux系统,则需要添加
using namespace std;
标题下方 如果是Windows,请确保正确放置标题
#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;
}
    

要回复问题请先登录注册