1

I am trying to use a solution from this question:

The error message

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)

(and a bunch of template trace data after this)

I am using Visual C++ 2010 Express.

The code

#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));
}
Community
  • 1
  • 1
Merlyn Morgan-Graham
  • 58,163
  • 16
  • 128
  • 183

2 Answers2

2

Here is correct version that compiles fine :

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

The conversion operator needs to be const.

BЈовић
  • 62,405
  • 41
  • 173
  • 273
2

I changed:

  operator std::string()

To

operator std::string() const

and it compiled fine.

littleadv
  • 20,100
  • 2
  • 36
  • 50