大家好,我正在尝试重载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;
}
};
Run Code Online (Sandbox Code Playgroud)
测试文件:
#include <iostream>
#include <fstream>
#include <string>
#include "Complex2.h" …Run Code Online (Sandbox Code Playgroud)