简体   繁体   中英

C++ ifstream and ofstream overloading operator reading from file

Hey guys I am trying to overload ifstream and ofstream but without any success.

Header File:

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

};

Test file:

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

I didnt think the cpp file was important since the header file includes the ifstream.

Everytime I run this program i get this error:

Reading from the file
Segmentation fault (core dumped)

I dont know how to fix it.

Thank you very much.

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

    return in;
}

The above code implements the operator>> for the Complex type. However, it is a recursive function with no stopping condition.

You probably should be do something similar to the following. Obviously, I don't know how the class was encoded so this is just for illustration.

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

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

    return in;
}

I overlooked that it's a friend function so this could also work.

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

    return in;
}

As mentioned in my comment, and in the other answer the operator>>() overload is just called recursively.


One of the most common approaches to fix that, is to declare a put() function in your Complex class like:

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

And let the global overload call that function:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM