微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决为什么我不能发出字符串?

| 为什么我不能这样
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;
}
    

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。