【问题标题】:error C2679: binary '<<' : no operator found which takes a right-hand operand of type “std::string” [closed]错误 C2679:二进制“<<”:未找到采用“std::string”类型的右侧操作数的运算符 [关闭]
【发布时间】:2011-12-30 12:32:39
【问题描述】:

代码:

    #include "stdafx.h"
    #include <iostream>
    #include <fstream>
    #include <string.h>

    using namespace std;
    int main()
        {
            ifstream fin ("ride.in.txt");
            ofstream fout ("ride.out.txt");
            int ta, tb;unsigned int i;
            ta = tb = 1;
            string a, b;
            fin >> a >> b;
            for (i = 0; i < a.size(); i++)
                ta = ta * (a[i] - 'A' + 1) % 47;
            for (i = 0; i < b.size(); i++)
                tb = tb * (b[i] - 'A' + 1) % 47;
            if (ta == tb)
                fout << "GO" << endl;
            else    
                fout << "STAY" << endl;
            return 0;
        }

错误:

error C2679: 
binary '<<' : no operator found which takes a right-hand operand of type  “std::string”

【问题讨论】:

  • 没有问题,代码在clang上编译(去除未提供的预编译头时)。
  • 它将在库发生&lt;fstream&gt;&lt;iostream&gt; 中包含&lt;string&gt; 的任何编译器上编译。一个 C++ 标准头文件允许包含任何其他标准头文件。
  • 我不明白为什么它被关闭了。它一个真实的问题,并且有答案。无论如何,有一个类似问题的答案here

标签: c++ string io operators


【解决方案1】:

我认为问题是:

#include <string.h>

改为:

#include <string>

【讨论】:

    【解决方案2】:

    std::string 运算符在 &lt;string&gt; 标头中定义。

    标头 &lt;string.h&gt; 用于 C 风格的字符串函数。

    【讨论】:

    • 实际上,在 C++ 中,标题 &lt;string.h&gt; 甚至都不存在。可以在 &lt;cstring&gt; 中找到该名称的 C 标头的内容(包装在命名空间 std 中)。
    • 标头仍然是 C++ 标准的一部分,尽管它隐藏在 Annex D 中(兼容性功能/已弃用)。
    • 啊,谢谢指正!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多