【问题标题】:Why I cannot cout a string?为什么我不能找到一个字符串?
【发布时间】:2011-09-13 08:11:22
【问题描述】:

为什么我不能像这样cout string

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

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

错误 2 错误 C2679:二进制“

令人惊讶的是,即使这样也不起作用:

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

【问题讨论】:

  • 你能在错误信息中编辑吗?
  • #include &lt;iostream&gt;了吗?
  • 信息不足。什么是错误
  • 我已经做到了。但同样,我有问题。
  • 你能发布整个文件吗?我们不知道您是否在函数中调用它,是否包含了正确的内容等等......

标签: c++ string cout


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

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

      using namespace std;

      标题下方

      如果是 windows,请确保您正确放置标题 #include&lt;iostream.h&gt;

      #include&lt;string.h&gt;

      参考这个,它工作得很好。

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

      【讨论】:

      • using namespace std; 与目标操作系统为linux 无关。同样,将.h添加到include中与目标操作系统是windows无关,#include &lt;iostream&gt;#include &lt;string&gt;将在windows上运行。
      【解决方案4】:

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

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

      【讨论】:

      • 欢迎来到 StackOverflow,您可能没有注意到,但这已在已接受答案的 cmets 之一中得到解决。
      【解决方案5】:

      你的代码有几个问题:

      1. WordList 没有在任何地方定义。你应该在使用它之前定义它。
      2. 您不能只在这样的函数之外编写代码。你需要把它放在一个函数中。
      3. 在使用coutendl之前,您需要先#include &lt;string&gt;才能使用字符串类和iostream。
      4. stringcoutendl 位于 std 命名空间中,因此除非您使用 using 指令首先将它们带入范围,否则如果不使用 std:: 前缀就无法访问它们。

      【讨论】:

      • 没有一个对我有用,似乎问题出在 substr 上
      【解决方案6】:

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

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

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

      【讨论】:

        【解决方案7】:

        你需要包含

        #include <string>
        #include <iostream>
        

        【讨论】:

        • 还有using namespace stdusing std::coutusing std::endl;
        • 是的,但我想它已包含在内,因为 string text; 上没有错误,编辑(添加错误)也说,这不是问题,而是缺少 string 标头。
        • +1:Visual C++ 中的许多 STL 标头(包括 )引入了 std::basic_string 类的定义(因为它们间接包含实现定义的 标头(永远不要直接包含它))。虽然这允许您使用字符串类,但相关的 operator&lt;&lt; 是在 标头本身中定义的,因此您必须手动包含它。同样依赖其他头文件来间接包含std::basic_string的定义在VC++中有效,但不适用于所有编译器。
        • Sven- 你的评论太棒了!我和这个提问者有类似的问题,编译器说运算符 >> 没有为类型 std::cin 和 std::string 定义。原来我有 但忘记了 。我习惯于在带有 gcc 的 linux 上工作,这会抱怨 std::string 没有定义。您的评论完美地解释了为什么我们反而收到了关于运营商的投诉。谢谢!!
        • 这行得通。我错过了代码中的 #include 行。谢谢。
        猜你喜欢
        • 2022-08-18
        • 2021-11-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-12-29
        • 2018-10-18
        相关资源
        最近更新 更多