c++ - 使用 std::copy - 错误 C2679:找不到正确的二进制 '=' 运算符

标签 c++ copy compiler-errors iostream

我正在尝试使用这个问题的解决方案:

错误信息

c:\program files (x86)\microsoft visual studio 10.0\vc\include\xutility(2144): error C2679: binary '=' : no operator found which takes a right-hand operand of type 'const Line' (or there is no acceptable conversion)

(以及之后的一堆模板跟踪数据)

我正在使用 Visual C++ 2010 Express。

代码

#include<string>
#include<iostream>
#include<fstream>
#include<vector>
#include<iterator>

class Line
{
  std::string data;

public:
  friend std::istream& operator>>(std::istream& inputStream, Line& line)
  {
    std::getline(inputStream, line.data);
    return inputStream;
  }

  operator std::string()
  {
    return data;
  }
};

int main(int argc, char* argv[])
{
  std::fstream file("filename.txt", std::fstream::in | std::fstream::out);
  std::vector<std::string> lines;

  // error is in one of these lines
  std::copy(
    std::istream_iterator<Line>(file),
    std::istream_iterator<Line>(),
    std::back_inserter(lines));
}

最佳答案

这是编译好的正确版本:

class Line
{
    std::string data;

    public:
        friend std::istream& operator>>(std::istream& inputStream, Line& line)
        {
            std::getline(inputStream, line.data);
            return inputStream;
        }

        operator std::string() const
        {
            return data;
        }
};

转换运算符需要是const

关于c++ - 使用 std::copy - 错误 C2679:找不到正确的二进制 '=' 运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6302899/

相关文章:

c++ - 有没有办法告诉 C++ 中指针实际指向什么类型

ios - 如何在 Objective-C 中使用 "copy"属性?

java - 在 Java 中将文件从一个目录复制到另一个目录

c++ - SDL2 在 Fedora 21 中调整 buggy 但在 Windows 中正常

c++ - 闪存数据格式

excel - 如何创建宏以将数据从一个excel工作表复制到另一个工作表的下一行

recursion - 堆栈递归程序问题

c++ - GCC 不将 __VA_ARGS__ 视为宏中的参数

c++ - 尾随返回类型中 SFINAE 的 GCC 错误

c++ - CMake:找不到头文件