使用std :: copy - 错误C2679:找不到正确的二进制'='运算符 [英] Using std::copy - error C2679: can't find correct binary '=' operator
问题描述
我尝试使用此问题的解决方案:
错误讯息
c:\program files(x86)\microsoft visual studio 10.0 \vc\include\xutility(2144):error C2679:binary'=':无操作符,它接受类型为 const Line'(或者没有可接受的转换)
b
$ b
我使用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;
}
操作符std :: string()
{
返回数据;
}
};
int main(int argc,char * argv [])
{
std :: fstream file(filename.txt,std :: fstream :: in | std :: fstream :: out);
std :: vector< std :: string>线;
//错误在这些行之一
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;
}
操作符std :: string()const
{
返回数据;
}
};
转换运算符需要 const
。
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));
}
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
.
这篇关于使用std :: copy - 错误C2679:找不到正确的二进制'='运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!