1

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!

+1

Ihr Kopierkonstruktor weist keinen Speicher für 'IntList' zu. – 0x499602D2

+0

Also habe ich meinen Kopierkonstruktor fast identisch mit meinem überladenen = Operator gemacht und ich habe immer noch dieses Problem. –

+0

@HarrisonHarward Warum retten Sie sich nicht vor all diesen Kopfschmerzen und verwenden 'std :: vector ' anstelle von 'int *'. – PaulMcKenzie

Antwort

0

Der einzige Grund, warum Sie Probleme mit Ihrem überladen operator+() und operator*() sehen, ist, dass sie nach Wert zurückgeben.

Rückgabe nach Wert bedeutet, eine Kopie von dem, was zurückgegeben wird, zu erstellen. Diese Kopie wird an den Aufrufer zurückgegeben und (normalerweise) schließlich zerstört.

Das eigentliche Problem ist dann in Ihrem Copykonstruktor

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

, die nicht IntList nicht initialisieren auf etwas zu zeigen, bevor sie in der Schleife zu IntList[i] zuweisen. Das Ergebnis ist ein undefiniertes Verhalten.

Da der von Ihren Funktionen zurückgegebene Wert schließlich nicht mehr existiert, wird der Destruktor aufgerufen.

MyInt::~MyInt(){ 

    delete [] IntList;    //deallocate memory 
} 

, die, auch weil IntList Ursachen undefiniertes Verhalten ist nicht initialisiert worden.

Big Hinweis: beide anderen Konstruktoren haben eine Aussage IntList = new char[current_size];. Logic könnte vorschlagen, dass der Kopierkonstruktor etwas Ähnliches benötigt.

+0

Vielen Dank! Ich schaue mir das jetzt an! –

+0

Ich habe die Zeile hinzugefügt, um den Speicher für IntList zuzuweisen, und ich bekomme immer noch den seg-Fehler. –

+0

Sie müssen alle Ihre Funktionen betrachten, um zu sehen, ob sie sich konsistent verhalten. Zum Beispiel weist Ihr Konstruktor, der ein "int" akzeptiert, "IntList" zweimal zu, kopiert jedoch keine Daten (z. B. vom ersten zugewiesenen Block zum zweiten) und gibt sie auch nicht frei. Der Operator >>() 'macht etwas ähnliches. Dies ist wahrscheinlich darauf zurückzuführen, dass Sie fehlerhaften Code an mehreren Stellen kopiert haben. Dies bedeutet, dass das Problem wiederholt auftritt. Sie müssen das selbst durcharbeiten - ein Q & A-Format eignet sich nicht zum Debuggen von Code mit mehreren Problemen wie diesem. – Peter

0

Weisen Sie in Ihrem Kopierkonstruktor den Speicher IntList zu.

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

Ihr Code in operator+ und operator* schafft ein neues Objekt r, deren Mitglieder nicht richtig initialisiert, und auch in Ihrem Copykonstruktor Mitglieder nicht ordnungsgemäß kopiert werden.

+0

Ich habe die Zeile hinzugefügt, um den Speicher für IntList zuzuweisen, und ich bekomme immer noch den Seg-Fehler. –

Verwandte Themen