C++ 为什么我不能cout一个字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6320995/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 19:54:41  来源:igfitidea点击:

Why I cannot cout a string?

c++stringcout

提问by Ata

Why I cannot coutstringlike this:

为什么我不coutstring喜欢这个:

string text ;
text = WordList[i].substr(0,20) ;
cout << "String is  : " << text << endl ;

When I do this, I get the following error:

当我这样做时,我收到以下错误:

Error 2 error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion) c:\users\mollasadra\documents\visual studio 2008\projects\barnamec\barnamec\barnamec.cpp 67 barnamec**

错误 2 错误 C2679:二进制“<<”:未找到采用“std::string”类型的右侧操作数的运算符(或没有可接受的转换) c:\users\mollasadra\documents\visual studio 2008\项目\barnamec\barnamec\barnamec.cpp 67 barnamec**

It is amazing, that even this is not working:

令人惊讶的是,即使这样也行不通:

string text ;
text = "hello"  ;
cout << "String is  : " << text << endl ;

回答by Kiril Kirov

You need to include

你需要包括

#include <string>
#include <iostream>

回答by nperson325681

You need to reference the cout's namespace stdsomehow. For instance, insert

您需要以std某种方式引用 cout 的命名空间。例如,插入

using std::cout;
using std::endl;

on top of your function definition, or the file.

在您的函数定义或文件之上。

回答by sepp2k

There are several problems with your code:

您的代码有几个问题:

  1. WordListis not defined anywhere. You should define it before you use it.
  2. You can't just write code outside a function like this. You need to put it in a function.
  3. You need to #include <string>before you can use the string class and iostream before you use coutor endl.
  4. string, coutand endllive in the stdnamespace, so you can not access them without prefixing them with std::unless you use the usingdirective to bring them into scope first.
  1. WordList没有在任何地方定义。你应该在使用它之前定义它。
  2. 你不能只是在这样的函数之外编写代码。你需要把它放在一个函数中。
  3. #include <string>在使用coutor之前,您需要先使用字符串类和 iostream endl
  4. string,cout并且endl位于std命名空间中,因此std::除非您先使用using指令将它们引入作用域,否则您无法在不使用前缀的情况下访问它们。

回答by Maheswar Reddy

Above answers are good but If you do not want to add string include, you can use the following

上面的答案很好,但如果你不想添加字符串包含,你可以使用以下

ostream& operator<<(ostream& os, string& msg)
{
os<<msg.c_str();

return os;
}

回答by Anthony.

Use c_str() to convert the std::string to const char *.

使用 c_str() 将 std::string 转换为 const char *。

cout << "String is  : " << text.c_str() << endl ;

回答by Ash Ghal

You do not have to reference std::coutor std::endlexplicitly.
They are both included in the namespace std. using namespace stdinstead of using scope resolution operator ::every time makes is easier and cleaner.

您不必引用std::coutstd::endl明确。
它们都包含在namespace std. using namespace std而不是::每次都使用范围解析运算符,这样更容易和更清晰。

#include<iostream>
#include<string>
using namespace std;

回答by pratikpchpr

If you are using linux system then you need to add

如果你使用的是linux系统,那么你需要添加

using namespace std;

using namespace std;

Below headers

标题下方

If windows then make sure you put headers correctly #include<iostream.h>

如果 Windows 则确保正确放置标题 #include<iostream.h>

#include<string.h>

#include<string.h>

Refer this it work perfectly.

参考这个它完美地工作。

#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;
}