简体   繁体   中英

How to read complex numbers from a file using class and problem with access to private variable in C++?

I get a task. I must read complex numbers from a file and write them into another file. In class, I must create two private variables for the real number and imaginary number. A little bigger function I must create outside class as inline functions.

This is my code:

#include <iostream>
#include <fstream>
#include <cstdlib> 
#include <assert.h>

using namespace std;

class Complex_num {
    double real, imag;
public:
    Complex_num(){
    real=0;
    imag=0;
    }


    friend std::ostream& operator<<(std::ostream&, const Complex_num&);
    friend std::istream& operator>>(std::istream&, Complex_num&);
};


inline void readC(FILE *wp, double *real, double *imag) {
    char c;
    assert(fscanf(wp,"%c%lg%c%lg%c%c",&c,real,&c,real,&c,&c));
}
inline int writeC(FILE *wp, double real, double imag)
{    
    fprintf(wp, "%.2f, %.2f\n", real, imag);

    return 1;
}


int main(int argc, char* argv[])
{
    FILE *wz, *wc;  

    char c;
    ifstream read(argv[1]);
    if (!read)
        { cerr << "Open error: " << argv[1] << endl; exit(1);}
    ofstream write(argv[2]);
    if(!write) { cerr << "Open error: " << argv[2] << endl; exit(2);}
    while(read.get(c)) 
        write.put(c);

    Complex_num x1;
    readC(wz, x1.real, x1.imag);
    write(wc, x1.real, x1.imag);



    return 0;
} 

I have some errors

czyt_pisanie.cpp: In function ‘int main(int, char**)’:
czyt_pisanie.cpp:48:18: error: ‘double Complex_num::real’ is private within this context
     readC(wz, x1.real, x1.imag);
                  ^~~~
czyt_pisanie.cpp:9:12: note: declared private here
     double real, imag;
            ^~~~
czyt_pisanie.cpp:48:27: error: ‘double Complex_num::imag’ is private within this context
     readC(wz, x1.real, x1.imag);
                           ^~~~
czyt_pisanie.cpp:9:18: note: declared private here
     double real, imag;
                  ^~~~
czyt_pisanie.cpp:48:31: error: cannot convert ‘double’ to ‘double*’ for argument ‘2’ to ‘void readC(FILE*, double*, double*)’
     readC(wz, x1.real, x1.imag);
                               ^
czyt_pisanie.cpp:49:18: error: ‘double Complex_num::real’ is private within this context
     write(wc, x1.real, x1.imag);
                  ^~~~
czyt_pisanie.cpp:9:12: note: declared private here
     double real, imag;
            ^~~~
czyt_pisanie.cpp:49:27: error: ‘double Complex_num::imag’ is private within this context
     write(wc, x1.real, x1.imag);
                           ^~~~
czyt_pisanie.cpp:9:18: note: declared private here
     double real, imag;
                  ^~~~
czyt_pisanie.cpp:49:31: error: no match for call to ‘(std::ofstream {aka std::basic_ofstream<char>}) (FILE*&, double&, double&)’
     write(wc, x1.real, x1.imag);

I also have operators, but I don't know what I should do with them

    friend std::ostream& operator<<(std::ostream&, const Complex_num&);
    friend std::istream& operator>>(std::istream&, Complex_num&);

You have declared two friend operators that can access the private variables. So your code should look something like this

std::ostream& operator<<(std::ostream& out, const Complex_num& c)
{
    return out << c.real << ' ' << c.imag;
}

std::istream& operator>>(std::istream& in, Complex_num& c)
{
    // code to read c.real and c.imag
    return in;
}

int main()
{
    ...
    Complex_num x1;
    read >> x1;
    write << x1;
}

Well, you can't access the private variables on your class, because they are PRIVATE. You are out of their context on the main function, create a public getter function like this on ComplexNumber class:

class Complex_num {
    double real, imag;
public:
    Complex_num(){
    real=0;
    imag=0;
    }
    double getReal(){ return real; }
};

Alternativelly you can just make the two variables public. You also should change the readC and writeC to receive a ComplexNumber object as parameter, and then use the getReal() method inside of it.

About the file reading, stop using the C way of writing and reading a file. They provided you a operator override to read ComplexNumber directly from a file, something like this:

  ofstream outputfile;
  outputfile.open ("example.txt");
  /* Check for opening */ 
  outputfile << _complexNumber;
  outputfile.close();

  ifstream inputfile;
  inputfile.open ("example.txt");
  /* Check for opening */ 
  inputfile >> _complexNumber;
  inputfile.close();

Having _complexNumber as the function parameter.

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