VC驿站

 找回密码
 加入驿站

QQ登录

只需一步,快速开始

搜索
查看: 1008|回复: 8

[讨论] 后置递增运算符重载和左移运算符重载公用时报错

[复制链接]
10_avatar_middle
最佳答案
0 
在线会员 发表于 2021-6-24 21:27:47 | 显示全部楼层 |阅读模式
本帖最后由 xiaoye1410 于 2021-6-24 21:33 编辑

我是一个C++的初学者,在学习到递增运算符重载时遇到了困难,希望有前辈帮忙看一下,感激不尽!!!
前置递增运算符正常,后置时报图片中的错误,上网并未查到解决办法。
#include <iostream>
using namespace std;
class myInteger
{
        friend ostream& operator<<(ostream& cout, myInteger& Inte);
public:
        myInteger()
        {
                m_int = 0;
        }
        //前置++运算符重载
        myInteger& operator++()
        {
                m_int++;
                return *this;
        }
        //后置++运算符重载
        myInteger operator++(int)
        {
                myInteger temp = *this;
                m_int++;
                return temp;
        }
private:
         int m_int;
};

ostream& operator<<(ostream& cout, myInteger& inte)
{
        cout << inte.m_int;
        return cout;
}

void test01()
{
        myInteger Inte;
        cout << ++Inte << endl;
        cout << ++(++Inte) << endl;
        cout << Inte << endl;

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

int main()
{
        test01();
        system("pause");
        return 0;
}
图片1.png




上一篇:用C++做工控开发 输入延时,IO延时
下一篇:vc++读写excel获取sheets容器
17_avatar_middle
最佳答案
40 
在线会员 发表于 2021-6-25 08:51:14 | 显示全部楼层
xiaoye1410 发表于 2021-6-25 08:40
感谢您的回复,使用您说的方法,跟图片1报错是一样的

vs2019,,运行通过,,加个 const

  1. #include <iostream>
  2. using namespace std;
  3. class myInteger
  4. {
  5.     friend ostream& operator<<(ostream& cout, const myInteger& Inte);
  6. public:
  7.     myInteger() {
  8.         m_int = 0;
  9.     }
  10.     //前置++运算符重载
  11.     myInteger& operator++() {
  12.         m_int++;
  13.         return *this;
  14.     }
  15.     //后置++运算符重载
  16.     myInteger operator++(int) {
  17.         myInteger temp = *this;
  18.         m_int++;
  19.         return temp;
  20.     }
  21. private:
  22.     int m_int;
  23. };

  24. ostream& operator<<(ostream& cout, const  myInteger& inte) {
  25.     cout << inte.m_int;
  26.     return cout;
  27. }

  28. void test01() {
  29.     myInteger Inte;
  30.     cout << ++Inte << endl;
  31.     cout << ++(++Inte) << endl;
  32.     cout << Inte << endl;

  33.     cout << Inte++ << endl;
  34.     cout << Inte << endl;
  35.     cout << (Inte++)++ << endl;
  36. }

  37. int main() {
  38.     test01();
  39.     system("pause");
  40.     return 0;
  41. }
复制代码

评分

参与人数 1驿站币 +1 热心值 +1 收起 理由
10_avatar_small xiaoye1410 + 1 + 1

查看全部评分

10_avatar_middle
最佳答案
0 
ico_lz  楼主| 发表于 2021-6-24 21:29:47 | 显示全部楼层
错误(活动)        E0349        没有与这些操作数匹配的 "<<" 运算符        递增运算符重载
10_avatar_middle
最佳答案
0 
ico_lz  楼主| 发表于 2021-6-24 21:31:19 | 显示全部楼层
错误(活动)        E0349        没有与这些操作数匹配的 "<<" 运算符        递增运算符重载    源.cpp        45       
10_avatar_middle
最佳答案
0 
ico_lz  楼主| 发表于 2021-6-24 21:50:07 | 显示全部楼层
经过排查,return指针类型的数据不报错但是,又有了一个新错误
图片2.png
10_avatar_middle
最佳答案
0 
ico_lz  楼主| 发表于 2021-6-24 22:06:03 | 显示全部楼层
如图修改可以正常运行,但是明显结果与理想不符,希望有经验的大神能够指点一下!!!
图片3.png
17_avatar_middle
最佳答案
40 
在线会员 发表于 2021-6-25 00:13:53 | 显示全部楼层
这样试试
  1.     myInteger operator++(int) {
  2.         myInteger temp = *this;
  3.         this->m_int++;
  4.         return temp;
  5.     }
复制代码
10_avatar_middle
最佳答案
0 
ico_lz  楼主| 发表于 2021-6-25 08:40:29 | 显示全部楼层

感谢您的回复,使用您说的方法,跟图片1报错是一样的
10_avatar_middle
最佳答案
0 
ico_lz  楼主| 发表于 2021-6-25 10:04:18 | 显示全部楼层
yoobaby 发表于 2021-6-25 08:51
vs2019,,运行通过,,加个 const

感谢老哥!!!
您需要登录后才可以回帖 登录 | 加入驿站 qq_login

本版积分规则

×【发帖 友情提示】
1、请回复有意义的内容,请勿恶意灌水;
2、纯数字、字母、表情等无意义的内容系统将自动删除;
3、若正常回复后帖子被自动删除,为系统误删的情况,请重新回复其他正常内容或等待管理员审核通过后会自动发布;
4、感谢您对VC驿站一如既往的支持,谢谢合作!

关闭

站长提醒上一条 /2 下一条

QQ|小黑屋|手机版|VC驿站 ( 辽ICP备09019393号-4 )|网站地图wx_jqr

GMT+8, 2023-6-10 05:21

Powered by CcTry.CoM

© 2009-2021 cctry.com

快速回复 返回顶部 返回列表