c++ - 我的程序因运算符过载而不断出现段错误

标签 c++ segmentation-fault overloading operator-keyword bus-error

头文件

#include <iostream>
#include <iomanip>
#include <cstring>
#include <cctype>
using namespace std;

class MyInt
{
  friend ostream& operator<< (ostream& out, const MyInt& myint);
  friend istream& operator>> (istream& in, MyInt& myint);
  friend istream& getline (istream& , MyInt& , char delim = '\n');

  friend MyInt operator+ (const MyInt& x, const MyInt& y);
  friend MyInt operator* (const MyInt& x, const MyInt& y);

  friend MyInt operator++(MyInt& x, int y);     //post-increment ++
  friend MyInt operator++(MyInt& x);            //pre-increment ++
  friend MyInt operator--(MyInt& x);            //pre-decrement --
  friend MyInt operator--(MyInt& x, int y);     //post-decrement --

  friend bool operator< (const MyInt& x, const MyInt& y);
  friend bool operator> (const MyInt& x, const MyInt& y);
  friend bool operator<=(const MyInt& x, const MyInt& y);
  friend bool operator>=(const MyInt& x, const MyInt& y);
  friend bool operator==(const MyInt& x, const MyInt& y);
  friend bool operator!=(const MyInt& x, const MyInt& y);
public:
  MyInt(int n=0);                           // default constructor
  MyInt(const char* number);               // c-style string with digits

  ~MyInt();                          // destructor

  MyInt(const MyInt& );           // copy constructor
  MyInt& operator=(const MyInt& );   // assignment operator
  //MyInt& operator+(const MyInt& x);
  //MyInt& operator*(const MyInt& x);
  int getLength() const;                // return int length



private:

  int current_size;
  int actual_number;
  int max_size;
  char * IntList;
};

类声明

    #include <iostream>
    #include <iomanip>
    #include <cstring>
    #include <cctype>
    #include "myint.h"
    using namespace std;

// friend functions (operatior overloads)

  ostream& operator << (ostream& out, const MyInt& myint){
        for(int i = 0; i < myint.current_size; i ++)
        {
                out << myint.IntList[i];
        }
        return out;
  }
  istream& operator >> (istream& in, MyInt& myint){
        int input;
        in >> input;
        cout << "Input: " << input << endl;
        int digits = 10;
        myint.current_size = 1;
        myint.IntList = new char[myint.current_size];
        myint.IntList[0] = '0';

        myint.actual_number = input;
        if(input >= 0 ){
                int length = 0;
                for(int x = input; x!=0; x /= 10)
                         length++;

                myint.current_size = length;
                for(int i = 0; i < length-2; i++)
                        digits *= 10;
                myint.IntList = new char[myint.current_size];

                myint.IntList[0] = '0' + (input/digits);
                for(int i = length-1; i > 0; i--){
                        myint.IntList[i] = '0' + (input % 10);
                        input/=10;
                }


        }

        return in;
  }


  MyInt::MyInt(int n){
        actual_number = 0;
        int digits = 10;
        current_size = 1;
        IntList = new char[current_size];
        IntList[0] = '0';

        if(n >= 0 ){
                actual_number = n;
                int length = 0;
                for(int x = n; x!=0; x /= 10)
                         length++;

                current_size = length;
                for(int i = 0; i < length-2; i++)
                        digits *= 10;
                IntList = new char[current_size];

                IntList[0] = '0' + (n/digits);
                for(int i = length-1; i > 0; i--){
                        IntList[i] = '0' + (n % 10);
                        n/=10;
                }


        }


  }

  MyInt::MyInt(const char* num){            // conversion from c-string

        current_size = strlen(num);
        IntList = new char[current_size];
        int count = 0;
        int size = 0;
        int j = 0;
        while(isdigit(num[j])){
                count++;
                j++;
        }
       for(int i = 0; i <count; i++)
                IntList[i] = num[i];
       sscanf(IntList, "%d", actual_number);
  }

  MyInt::~MyInt(){

        delete [] IntList;              //deallocate memory
  }

  MyInt::MyInt(const MyInt& copy_int){
        max_size = copy_int.max_size;
        current_size = copy_int.current_size;


        for(int i = 0; i < current_size; i++)
                IntList[i] = copy_int.IntList[i];
  }

   MyInt operator+ (const MyInt& x, const MyInt& y){
        cout << "Adding Ints: " << endl;
        MyInt r;
        r.actual_number = y.actual_number + x.actual_number;
        return r;
  }
  MyInt operator* (const MyInt& x, const MyInt& y){
        cout << "Multiplying Ints: " << endl;
        MyInt r;
        r.actual_number = y.actual_number * x.actual_number;
        return r;

   }

  bool operator< (const MyInt& x, const MyInt& y){
        if(x.actual_number < y.actual_number)
                return true;
        else
                return false;
  }
  bool operator> (const MyInt& x, const MyInt& y){
        cout << "x: " << x.actual_number << "y:" << y.actual_number << endl;
        if(x.actual_number > y.actual_number)
                return true;
        else
                return false;
  }
  bool operator<= (const MyInt& x, const MyInt& y){
        if(x.actual_number <= y.actual_number)
                return true;
        else
                return false;
  }
  bool operator>= (const MyInt& x, const MyInt& y){
        if(x.actual_number >= y.actual_number)
                return true;
        else
                return false;
  }
  bool operator==(const MyInt& x, const MyInt& y){
        if(x.actual_number == y.actual_number)
                return true;
        else
                return false;
  }
  bool operator!=(const MyInt& x, const MyInt& y){
        if(x.actual_number != y.actual_number)
                return true;
        else
                return false;
  }


 MyInt& MyInt::operator=(const MyInt& assign_int ){
        if(this != &assign_int)
        {    
                delete [] IntList;
                this->actual_number = assign_int.actual_number;
                cout << "actual num: " << this->actual_number << endl;

                int digits = 10;
                current_size = 1;
                IntList = new char[current_size];
                IntList[0] = '0';

                if(actual_number >= 0 ){
                        int length = 0;
                        for(int x = actual_number; x!=0; x /= 10)
                                length++;

                        current_size = length;
                        for(int i = 0; i < length-2; i++)
                                digits *= 10;
                        IntList = new char[current_size];

                        IntList[0] = '0' + (actual_number/digits);
                        for(int i = length-1; i > 0; i--){
                                IntList[i] = '0' + (actual_number % 10);
                                actual_number/=10;
                        }
                }

        }
        return *this;

  }

问题出在我重载的 + 和 * 运算符上。我已经尝试了我能想到的一切。我得到的错误是 Segmentation Fault (core dumped) 或 Bus Error (core dumped)。我知道这一定是我没有看到的东西,感谢任何帮助!!谢谢!

最佳答案

您看到重载的 operator+()operator*() 出现问题的唯一原因是它们按值返回。

按值返回意味着创建返回的任何内容的拷贝。该拷贝将返回给调用者并(通常)最终销毁。

真正的问题是在你的拷贝构造函数中

MyInt::MyInt(const MyInt& copy_int){
    max_size = copy_int.max_size;
    current_size = copy_int.current_size;


    for(int i = 0; i < current_size; i++)
            IntList[i] = copy_int.IntList[i];
}

在循环中分配给 IntList[i] 之前,它不会初始化 IntList 以指向任何东西。这样做的结果是未定义的行为。

由于您的函数返回的值最终将不复存在,因此将调用其析构函数。

MyInt::~MyInt(){

    delete [] IntList;              //deallocate memory
}

这也是因为 IntList 尚未初始化导致未定义的行为。

大提示:您的其他构造函数都有一个语句 IntList = new char[current_size];。逻辑可能表明复制构造函数需要类似的东西。

关于c++ - 我的程序因运算符过载而不断出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36230856/

相关文章:

c++ - 您查找段错误原因的方法是什么?

Java 方法重载最佳实践

c++ - 为什么operator>>(或<<)重载函数需要接收i\ostream引用?

sum - Scheme编程求和函数重载

c++ - OpenMP 线性加速

c++ - 在 C++ 中测量调用 system() 的执行时间

c++ - "Process terminated with status -1073741819"带 vector 的简单程序

c - 在结构中使用 posix_memalign 出现段错误

c++ - OpenMP任务看不到共享变量的更改

c++ - OpenCV中的段错误