编译器错误 C2679:没有找到接受“MyInteger”类型的右操作数的运算符(或没有可接受的转换)

博客介绍了C++编程中遇到的编译器错误C2679,该错误源于后置递增运算符的使用。在重载后置递增运算符时,由于返回的是临时对象,而友元函数的`operator<<`参数为普通引用,导致无法接受。解决方案是将`operator<<`的参数类型更改为常量引用。
#include <iostream>
#include <string>
#include <vector>
using namespace std;

class MyInteger {
	friend ostream& operator<<(ostream& cout, MyInteger& myint);
private:
	int m_A;
public:
	MyInteger() {
		m_A = 0;
	}

	// 重载前置 ++ 运算符
	MyInteger& operator++() {
		++m_A;
		return *this;
	}

	// 重载后置++运算符 先将当前值返回,再进行递增运算
	 MyInteger operator++(int) {
		// 记录当前值
		MyInteger temp = *this;
		++*this;
		return temp;
	}
};

ostream& operator<<(ostream& cout, MyInteger& myint) {
	cout << myint.m_A;
	return cout;
}

void test01() {
	MyInteger myint;

	cout << ++(++myint) << endl;
	cout << myint << endl;
}

void test02() {
	MyInteger myint;
	cout << myint++ << endl;
	cout << myint << endl;
}

int main() {
	//test01();
	test02();

	//int& a = 10;
	//const int& b = 10;

	system("pause");
	return 0;
}

报错信息:

原因在于后置递增运算符:
为了区分前置递增运算符,在重载后置递增运算符的形参列表中添加了int占位符,而且由于后置递增不允许链式编程,其返回值类型为值类型,即返回一个临时对象,但是由于重载operator<<函数中接收的形参为普通引用,不能接收临时对象,所以才产生了这样的错误。

更改重载operator<<函数为(MyInteger类中友元函数声明中也要改):

ostream& operator<<(ostream& cout, const MyInteger& myint) {
	cout << myint.m_A;
	return cout;
}

将参数类型改为常量引用就能够正常输出了。

举例:

要注意区分普通引用和常量引用,最简单的例子就是:

	int& a = 10;    // 错误
	const int& b = 10; // 正确

对应到这个例子中:

	MyInteger& m1 = MyInteger();  // 错误,普通引用不能接收临时对象
	const MyInteger& m2 = MyInteger(); // 正确 常量引用能接收此临时对象
请告知以下代码这个编译错误表示编译器找不到匹配的 << 运算符重载函数来处理 std::ostream 和你的自定义类型 myinTeger的来源#include<iostream> #include<string> using namespace std; //4.5.3 递增运算符重载 //作用:通过重载递增运算符,实现自己的整型数据 // 帮助编译器知道如何++自定义的数据类型 //自定义整型 class myinTeger{ friend ostream& operator<<(ostream &cout,myinTeger &int1); public: myinTeger(); //成员函数重载前置++运算符声明 myinTeger& operator++(); //返回应用是链式编程 为了对一个数据进行操作 //成员函数重载后置++运算符声明 myinTeger operator++(int); //发生函数重载 用占位参数int加以区分 //由于成员属性只有一个int类型的 所以只填int 不能填其他类型 private: int m_Num; }; myinTeger::myinTeger() //默认构造函数调用 { cout<<"myinTeger default f(X) ing!"<<endl; m_Num = 0; } //前置递增 先进行自增 再完成表达式 对应函数体先自加运算再返回 myinTeger& myinTeger::operator++() //成员函数对对象自身的成员属性访问不需要再传参进来 { //先做运算 m_Num++; //再完成返回 return *this; //解引用得到的是地址下存放的数据(调用该运算符的对象) //我要返回的是这个对象本身 而不是复制出来的副本 所以 //返回值类型myinTeger 返回的是对象的引用 同地址 可做链式编程 } //后置递增 先进行表达式 再完成自增 对应函数体先返回后自增 //但任何函数体执行return操作后 后面的代码都不会执行 表示这个函数运算已经有结果了 //成员函数重载后置++运算符声明 myinTeger myinTeger::operator++(int) { //不能先执行return 所以做记录操作 myinTeger temp = *this; //由于后置递增要用临时对象记录调用对象 这是一个函数体内的局部变量 //不能返回引用 因为函数体一旦执行完毕后它会被立刻释放 所以只能用值返 创建新的副本 m_Num++; return temp; } ostream& operator<<(ostream &cout,myinTeger &int1) //仅全局函数可实现左移运算符重载 { cout<<endl; cout<<"m_Num = "<<int1.m_Num<<endl; return cout; } void test01() { myinTeger myint; cout<< ++myint<<endl; cout<< myint <<endl; } void test02() { myinTeger myint; cout<<myint++<<endl; cout<<myint<<endl; } int main() { test01(); test02(); system("pause"); return 0; }
09-01
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值