2016-07-06 27 views
-2

Hey Leute, ich versuche, Istream und ofstream zu überlasten, aber ohne Erfolg.C++ ifstream und ofstream überladen Operator aus Datei lesen

Header-Datei:

#include <iostream> 
#include <fstream> 

using namespace std; 

class Complex 
{ 
    private: 
     double real; 
     double imaginary; 
    public: 
     //constructors 
     Complex(); 
     Complex(double newreal, double newimaginary); 
     ~Complex(); 
     //setter 
     void setReal(double newreal); 
     void setImaginary(double newimaginary); 
     //getter 
     double getReal(); 
     double getImaginary(); 
     //print 
     void print(); 
     //methods 
     Complex conjugate(); 
     Complex add(Complex c2); 
     Complex subtraction(Complex c2); 
     Complex division(Complex c2); 
     Complex multiplication(Complex c2); 

friend ifstream& operator >> (ifstream& in, Complex &c1) 
{ 

    in >> c1; 

    return in; 
} 

}; 

Testdatei:

#include <iostream> 
#include <fstream> 
#include <string> 
#include "Complex2.h" 

using namespace std; 

int main() 
{ 
    Complex c1; 

    ifstream infile; 
    infile.open("in1.txt"); 

    cout << "Reading from the file" << endl; 
    infile >> c1; 

    // write the data at the screen. 
    infile.close(); 

    return 0; 
} 

ich nicht glaube, dass die CPP-Datei wichtig war, da die Header-Datei die ifstream enthält.

Jedesmal, wenn ich dieses Programm ausführen bekomme ich diesen Fehler:

Reading from the file 
Segmentation fault (core dumped) 

Ich weiß nicht, wie es zu beheben.

Vielen Dank.

+1

'in >> c1;' ruft einfach die Operatorfunktionsdefinition rekursiv auf, bis der Stack erschöpft ist. –

Antwort

2
friend ifstream& operator >> (ifstream& in, Complex &c1) 
{ 
    in >> c1; // This is calling this function infinitely!! 

    return in; 
} 

Der obige Code implementiert die operator>> für die Complex Typ. Es ist jedoch eine rekursive Funktion ohne Abbruchbedingung.

Sie sollten wahrscheinlich etwas ähnliches wie folgt tun. Offensichtlich weiß ich nicht, wie die Klasse codiert wurde, so ist dies nur zur Veranschaulichung.

friend ifstream& operator >> (ifstream& in, Complex &c1) 
{ 
    double real; 
    in >> real; 
    c1.setReal(real); 

    double imaginary; 
    in >> imaginary; 
    c1.setImaginary(imaginary); 

    return in; 
} 

ich übersehen, dass es ein Freund Funktion ist so könnte dies auch funktionieren.

friend ifstream& operator >> (ifstream& in, Complex &c1) 
{ 
    in >> c1.real; 
    in >> c1.imaginary; 

    return in; 
} 
+0

Ich würde die Kapselung für das Parsen der privaten Daten bevorzugen. Es sollte wahrscheinlich mit einer 'privaten' Klassenmemberfunktion gemacht werden. –

+0

@ πάνταῥεῖ Keine Argumente hier, +1. –

1

Wie in meinem Kommentar erwähnt, und in der anderen Antwort der operator>>() Überlastung nur rekursiv aufgerufen.


Eines der am häufigsten verwendeten Ansätze, dies zu beheben, ist eine put() Funktion in Ihrem Complex Klasse wie zu erklären:

class Complex { 
public: 
    // ... 
private: 
    double real; 
    double imaginary; 
    istream& put(std::istream& is) { 
     is >> real; 
     is >> imaginary; 
     return is; 
    } 
}; 

und die globale Überlastung Aufruf dieser Funktion lassen:

friend ifstream& operator >> (ifstream& in, Complex &c1) { 
    return c1.put(in); 
}