ちょっと私はifstreamとofstreamをオーバーロードしようとしていますが、成功しません。ifstreamとofstreamオーバーロード演算子ファイルからの読み取り
ヘッダーファイル:
#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;
}
};
テストファイル:
#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;
}
私は、ヘッダファイルはifstreamを含むためのcppファイルが重要だったと思いdidntの。私はそれを修正する方法を知らない
Reading from the file
Segmentation fault (core dumped)
:私はこのプログラムを実行する
毎回私はこのエラーを取得します。
ありがとうございました。
'in >> c1;'はスタックが使い果たされるまで、単に演算子の関数定義を再帰的に呼び出します。 –