gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 13:18:34 25 4
gpt4 key购买 nike

头文件

#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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com