记录一下自己写代码出现的错误。
写关于复数的模板类时出现了这样的报错
错误 C2679 二进制“=”: 没有找到接受“const _Ty”类型的右操作数的运算符(或没有可接受的转换)
开始的代码如下:
#include
#include
#include
#include
using namespace std;
template<typename T>
class Complex {
public:
Complex() = default;
Complex(T a, T b):real(a),imag(b){ }
bool operator< (const Complex<T>& obj) {
return ((real * real + imag * imag) < (obj.real * obj.real + obj.imag * obj.imag)) ? true : false;
}
Complex<T>& operator=(const Complex<T>& obj);
friend ostream& operator << <>(ostream& os,const Complex<T>& obj);
friend istream& operator >> <>(istream& in,Complex<T>& obj);
void assign(T re, T im);
private:
T real = 0;
T imag = 0;
};
template<typename T>
istream& operator >> (istream& in, Complex<T>& obj)
{
in >> obj.real;
in>> obj.imag;
return in;
}
template<typename T>
ostream& operator<<(ostream& os, const Complex<T>& obj)
{
if (obj.real != 0) {
os << obj.real;
if (obj.imag != 0)
os << " + " << obj.imag << "i" << endl;
}
else if (obj.imag != 0) {
os << obj.imag << "i" << endl;
}
else {
os << "0" << endl;
}
return os;
}
template<typename T>
Complex<T>& Complex<T>::operator=(const Complex<T>& obj)
{
real = obj.real;
imag = obj.imag;
return *this;
}
template<typename T>
void Complex<T>::assign(T re, T im)
{
real = re;
imag = im;
}
int main() {
vector<Complex<int>> complex;
istream_iterator<int> in(cin);
istream_iterator<int> end_iter;
while(in!=end_iter) {
int re, im;
re = *in++;
im = *in++;
complex.push_back(Complex<int>(re,im));
}
sort(complex.begin(), complex.end());
copy(complex.begin(), complex.end(), ostream_iterator<Complex<int>>(cout, "\n"));
system("pause");
}
程序可以正常运行,这里是直接用循环读取然后赋值的,然后我想试一下用copy这个函数来直接读取进复数complex里。于是直接把这赋值语句注释掉然后用copy语句替代。main函数如下:
int main() {
vector<Complex<int>> complex;
istream_iterator<int> in(cin);
istream_iterator<int> end_iter;
//while(in!=end_iter) {
// int re, im;
// re = *in++;
// im = *in++;
// complex.push_back(Complex(re,im));
//}
copy(in, end_iter, back_inserter(complex));
sort(complex.begin(), complex.end());
copy(complex.begin(), complex.end(), ostream_iterator<Complex<int>>(cout, "\n"));
system("pause");
}
然后运行就会出现此报错
然后也一直也没找到哪里出错了,只知道是这个copy语句这无法正常进行,一直在上面类里找是不是重载有些问题。
最后解决发现,copy语句没有写错,其他构造也没有出错。错误出在
istream_iterator<int> in(cin);
istream_iterator<int> end_iter;
这里选择模板类型时应该是Complex而不是int,需要改过来,所以出现了无法匹配=的情况,无法将int的数据直接赋值给Complex。
修改后代码如下:
#include
#include
#include
#include
using namespace std;
template<typename T>
class Complex {
public:
Complex() = default;
Complex(T a, T b):real(a),imag(b){ }
bool operator< (const Complex<T>& obj) {
return ((real * real + imag * imag) < (obj.real * obj.real + obj.imag * obj.imag)) ? true : false;
}
Complex<T>& operator=(const Complex<T>& obj);
friend ostream& operator << <>(ostream& os,const Complex<T>& obj);
friend istream& operator >> <>(istream& in,Complex<T>& obj);
void assign(T re, T im);
private:
T real = 0;
T imag = 0;
};
template<typename T>
istream& operator >> (istream& in, Complex<T>& obj)
{
in >> obj.real;
in>> obj.imag;
return in;
}
template<typename T>
ostream& operator<<(ostream& os, const Complex<T>& obj)
{
if (obj.real != 0) {
os << obj.real;
if (obj.imag != 0)
os << " + " << obj.imag << "i" << endl;
}
else if (obj.imag != 0) {
os << obj.imag << "i" << endl;
}
else {
os << "0" << endl;
}
return os;
}
template<typename T>
Complex<T>& Complex<T>::operator=(const Complex<T>& obj)
{
real = obj.real;
imag = obj.imag;
return *this;
}
template<typename T>
void Complex<T>::assign(T re, T im)
{
real = re;
imag = im;
}
int main() {
vector<Complex<int>> complex;
istream_iterator<Complex<int>> in(cin);
istream_iterator<Complex<int>> end_iter;
//while(in!=end_iter) {
// int re, im;
// re = *in++;
// im = *in++;
// complex.push_back(Complex(re,im));
//}
copy(in, end_iter, back_inserter(complex));
sort(complex.begin(), complex.end());
copy(complex.begin(), complex.end(), ostream_iterator<Complex<int>>(cout, "\n"));
system("pause");
}