我的程序通过重载运算符来保持段错误

时间:2016-03-26 02:11:44

标签: 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;

  }

问题出在我的重载+和*运算符上。我已经尝试了我能想到的一切。我得到的错误是分段错误(核心转储)或总线错误(核心转储)。我知道它必须只是我没有看到的东西,任何帮助表示赞赏!谢谢!

2 个答案:

答案 0 :(得分:0)

您发现重载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之前,

不会初始化IntList[i]以指向任何内容。这样做的结果是未定义的行为。

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

MyInt::~MyInt(){

    delete [] IntList;              //deallocate memory
}

,因为IntList尚未初始化导致未定义的行为。

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

答案 1 :(得分:0)

将内存分配给复制构造函数中的IntList

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];
}

operator+operator*中的代码正在创建一个新对象r,其成员未正确初始化,并且复制构造函数成员也未正确复制。