[Error] cannot bind ‘std::ostream {aka std::basic_ostream<char>}‘ lva

5 篇文章 0 订阅

当我想自己实现通过重载递增运算符,实现自己的整形数据的递增时,出现了以下错误:

[Error] cannot bind 'std::ostream {aka std::basic_ostream<char>}' lvalue to 'std::basic_ostream<char>&&'

以下是运行错误的代码:

//递增运算符重载
#include<iostream>
using namespace std;

class MyInteger{
	friend ostream & operator <<(ostream &cout,MyInteger &p);
public:
	MyInteger(){
		m_Num=0;
	}
	//前置++
	MyInteger & operator ++(){
		m_Num++;
		return *this;
	}
	//后置++
	MyInteger operator ++(int){
		MyInteger temp=*this;//*this代表当前本身的值 
		m_Num++;
		return temp;
	} 
private:
	int m_Num; 
}; 

ostream & operator <<(ostream & cout,MyInteger &p){
	cout<<p.m_Num<<endl;
	return cout;
}
void text01(){
	MyInteger myint;
	cout<<++myint<<endl;
	cout<<myint<<endl;
}
void text02(){
	MyInteger myint;
	cout<<myint++<<endl;
	cout<<myint<<endl;
}
int main(){
	text01();
	text02();
	return 0;
}

 原因是在text02函数中,在运行myint++时返回的是temp,temp是一个局部变量,在栈区,运行结束后就会被释放掉,则如果用引用传递的话就会指向空内存,因此在ostream & operator <<(ostream & cout,MyInteger &p)函数中p不应该进行引用传递,应该进行值传递,所以把函数中的MyInteger &p改为MyInteger p即可。

初学者,如有错误还请各位大佬不吝赐教。

  • 6
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
You are visitor as of October 17, 1996.The Art of Assembly Language ProgrammingForward Why Would Anyone Learn This Stuff?1 What's Wrong With Assembly Language2 What's Right With Assembly Language?3 Organization of This Text and Pedagogical Concerns4 Obtaining Program Source Listings and Other Materials in This TextSection One: Machine OrganizationArt of Assembly Language: Chapter OneChapter One - Data Representation1.0 - Chapter Overview1.1 - Numbering Systems1.1.1 - A Review of the Decimal System1.1.2 - The Binary Numbering System1.1.3 - Binary Formats1.2 - Data Organization1.2.1 - Bits1.2.2 - Nibbles1.2.3 - Bytes1.2.4 - Words1.2.5 - Double Words1.3 - The Hexadecimal Numbering System1.4 - Arithmetic Operations on Binary and Hexadecimal Numbers1.5 - Logical Operations on Bits1.6 - Logical Operations on Binary Numbers and Bit Strings1.7 - Signed and Unsigned Numbers1.8 - Sign and Zero Extension1.9 - Shifts and Rotates1.10 - Bit Fields and Packed Data1.11 - The ASCII Character Set1.12 Summary1.13 Laboratory Exercises1.13.1 Installing the Software1.13.2 Data Conversion Exercises1.13.3 Logical Operations Exercises1.13.4 Sign and Zero Extension Exercises1.13.5 Packed Data Exercises1.14 Questions1.15 Programming ProjectsChapter Two - Boolean Algebra2.0 - Chapter Overview2.1 - Boolean Algebra2.2 - Boolean Functions and Truth Tables2.3 - Algebraic Manipulation of Boolean Expressions2.4 - Canonical Forms2.5 - Simplification of Boolean Functions2.6 - What Does This Have To Do With Computers, Anyway?2.6.1 - Correspondence Between Electronic Circuits and Boolean Functions2.6.2 - Combinatorial Circuits2.6.3 - Sequential and Clocked Logic2.7 - Okay, What Does It Have To Do With Programming, Then?2.8 - Generic Boolean Functions2.9 Laboratory Exercises<

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值