btthe 2024-02-27 20:28 采纳率: 0%
浏览 9

c++,重载递增运算符

重置++后置运算符,发生错误。
第47行有问题,求解答!

img

#include <string>
#include <iostream>
using namespace std;
//重载递增运算符
class MyInteger
{
    friend ostream& operator<<(ostream& cout, MyInteger& myint);
public:
    MyInteger()
    {
        m_Num = 0;
    }
    //重置前置++运算符 返回引用是为了一直对一个数据进行递增操作
    MyInteger& operator++()
    {
        //先进行++运算
        m_Num++;
        //再将自身做返回
        return *this;
    }
    //重置后置++运算符 加int与前置作区分
    MyInteger operator++(int) //后置返回值,因为temp是有作用域的;如果返回引用则是局部对象temp的引用,temp被释放掉
    {
        //先 记录当时结果
        MyInteger temp = *this;
        //再++
        this->m_Num++;
        //后 返回结果
        return temp;
    }
private:
    int m_Num;
};

//重载左移运算符
ostream& operator<<(ostream& cout, MyInteger& myint)
{
    cout << myint.m_Num;
    return cout;
}
void test3()
{
    MyInteger myint;
    cout << myint << endl;
    cout << ++myint << endl;
    cout << myint << endl;
    cout << myint++ << endl;
}
int main()
{
    test3();
    return 0;
}
  • 写回答

3条回答 默认 最新

  • GISer Liu 2024-02-27 20:43
    关注

    该回答引用自GPT-3.5,由博主GISer Liu编写:

    问题分析:
    在给定的代码中,重载了递增运算符(++),包括前置和后置两种形式。然而,在测试函数test3()中,对后置递增运算符的使用存在问题,导致运行时出错。具体来说,后置递增运算符在返回值上有一处问题,导致无法正确返回递增前的值。
    解决方案:

    1. 重载后置递增运算符时,应该返回一个值而不是引用。这是因为后置递增运算符必须返回原始值的副本,在执行递增操作之前。
    2. 在当前代码中,后置递增运算符返回的是MyInteger对象的引用,这是不正确的。应该修改为返回一个MyInteger对象的值。
    3. 为了正确返回递增前的值,在后置递增运算符内部,应该先记录当前对象的值到一个临时变量中,然后再执行递增操作。最后返回记录的临时变量,而不是原始对象。
      下面是修改后的代码:
      cppCopy code
      MyInteger operator++(int) // 后置递增运算符
      {
      // 先记录当前对象的值到一个临时变量中
      MyInteger temp = *this;
      // 执行递增操作
      this->m_Num++;
      // 返回记录的临时变量,即递增前的值
      return temp;
      }
      
      通过以上修改,可以保证后置递增运算符的正确性,并且测试函数test3()可以正确执行。
      完整代码如下:
      cppCopy code
      #include <string>
      #include <iostream>
      using namespace std;
      class MyInteger {
      friend ostream& operator<<(ostream& cout, MyInteger& myint);
      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& myint)
      {
      cout << myint.m_Num;
      return cout;
      }
      void test3()
      {
      MyInteger myint;
      cout << myint << endl;
      cout << ++myint << endl;
      cout << myint << endl;
      cout << myint++ << endl;
      }
      int main()
      {
      test3();
      return 0;
      }
      
      以上就是对问题的分析和解决方案,通过修改后置递增运算符的返回值类型和逻辑,可以解决后置递增运算符使用时的错误。

    如果该回答解决了您的问题,请采纳!如果没有,请参考以下方案进行修订

    用户答题指南

    评论

报告相同问题?

问题事件

  • 修改了问题 2月27日
  • 创建了问题 2月27日

悬赏问题

  • ¥20 python忆阻器数字识别
  • ¥15 无法输出helloworld
  • ¥15 高通uboot 打印ubi init err 22
  • ¥20 PDF元数据中的XMP媒体管理属性
  • ¥15 R语言中lasso回归报错
  • ¥15 网站突然不能访问了,上午还好好的
  • ¥15 有没有dl可以帮弄”我去图书馆”秒选道具和积分
  • ¥15 semrush,SEO,内嵌网站,api
  • ¥15 Stata:为什么reghdfe后的因变量没有被发现识别啊
  • ¥15 振荡电路,ADS仿真