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.
'in >> c1;' ruft einfach die Operatorfunktionsdefinition rekursiv auf, bis der Stack erschöpft ist. –