Header-DateiMein Programm hält seg mit überladenen Operatoren Verwerfungen
#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;
};
Klasse Erklärung
#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;
}
Das Problem mit meiner überlastet ist + und * Betreiber. Ich habe alles versucht, was mir einfiel. Der Fehler, den ich bekomme, ist entweder Segmentierungsfehler (Core Dumped) oder Bus Error (Core Dumped). Ich weiß, es muss nur etwas sein, was ich nicht sehe und jede Hilfe wird geschätzt !! Vielen Dank!
Ihr Kopierkonstruktor weist keinen Speicher für 'IntList' zu. – 0x499602D2
Also habe ich meinen Kopierkonstruktor fast identisch mit meinem überladenen = Operator gemacht und ich habe immer noch dieses Problem. –
@HarrisonHarward Warum retten Sie sich nicht vor all diesen Kopfschmerzen und verwenden 'std :: vector' anstelle von 'int *'. –
PaulMcKenzie