【问题标题】:Using std::copy - error C2679: can't find correct binary '=' operator使用 std::copy - 错误 C2679:找不到正确的二进制 '=' 运算符
【发布时间】:2011-06-10 06:48:39
【问题描述】:

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

错误信息

c:\program files (x86)\microsoft visual studio 10.0\vc\include\xutility(2144): error C2679: binary '=' : no operator found which take a right-hand operand of type 'const Line' (或没有可接受的转换)

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

我正在使用 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));
}

【问题讨论】:

标签: c++ copy compiler-errors iostream


【解决方案1】:

这是编译好的正确版本:

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

【讨论】:

    【解决方案2】:

    我变了:

      operator std::string()
    

    operator std::string() const
    

    它编译得很好。

    【讨论】:

      猜你喜欢
      • 2012-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多