错误 C2679 二进制“=”: 没有找到接受“const _Ty”类型的右操作数的运算符(或没有可接受的转换)的一种情况发生的错误

错误 C2679 二进制“=”: 没有找到接受“const _Ty”类型的右操作数的运算符(或没有可接受的转换

记录一下自己写代码出现的错误。

问题

写关于复数的模板类时出现了这样的报错
在这里插入图片描述
错误 C2679 二进制“=”: 没有找到接受“const _Ty”类型的右操作数的运算符(或没有可接受的转换)

开始的代码如下:

#include<iostream>
#include<vector>
#include<iterator>
#include<algorithm>

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<int>(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<iostream>
#include<vector>
#include<iterator>
#include<algorithm>

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<int>(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");
}

程序可以正常运行
在这里插入图片描述

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值