0

Before I start I would like to say that I don't consider myself a C++ programmer, but I have programmed in this language before (really basic stuff).

So, in this case I'm just trying to make an easy way to handle files like in Python or Java or any other high level language.

Why don't I do this in any other language?

I am using C++ instead of Python or Java, I am using it for its high performance.

Why don't I use any existing solution?

Well, I don't, because I really want to learn or at least to see how this works behind.


I have already searched for answers to my question, but even though I found, the explanations about streams can't be copied, but can be referenced: Why copying stringstream is not allowed?

Or how can I pass a stream as a parameter by reference: Pass a reference to std::ifstream as parameter

Even this which presents a question similar or the same as mine, to be honest I don't know what it refers to ( ":v ) open ofstream as class attribute

This is my code until now:

File.h


#include <fstream>
#include <string>

class File {
public:
    File(std::string const& filename, char mode);
    virtual ~File();

    void open();
    void close();

private:
    std::string filename;
    std::ios_base::openmode mode;
    std::fstream object; // this line causes errors
};
#endif /* FILE_H_ */

File.cpp


#include "File.h"
#include <fstream>
#include <string>

File::File(std::string const& filename, char mode) {
    this->filename = filename;

    switch (mode)  {
        case 'b':
            this->mode = std::ios::binary;
            break;

        case 'r':
            this->mode = std::ios::in;
            break;

        case 'w':
            this->mode = std::ios::out;
            break;
        default:
            exit(0);
    }
}

File::~File() {}
void File::open() {}
void File::close() {}

main.cpp


#include "File.h"
#include <iostream>


int main(int argc, char const *argv[])
{
    File file = File("header.txt", 'r');
    // file.close();
    return 0;
}

Output

main.cpp: In function ‘int main(int, const char**)’:
main.cpp:14:36: error: use of deleted function ‘File::File(const File&)’
   14 |  File file = File("header.txt", 'r');
      |                                    ^
File.h:17:7: note: ‘File::File(const File&)’ is implicitly deleted because the default definition would be ill-formed:
   17 |  virtual ~File();
      |       ^~~~
File.h:17:7: error: use of deleted function ‘std::basic_fstream<_CharT, _Traits>::basic_fstream(const std::basic_fstream<_CharT, _Traits>&) [with _CharT = char; _Traits = std::char_traits<char>]’
In file included from File.h:12:
/usr/include/c++/9/fstream:1104:7: note: declared here
 1104 |       basic_fstream(const basic_fstream&) = delete;

Thank you very much for your time! I hope you can help me. :)

jp72924
  • 3
  • 2
  • The problem is that stream objects can't be copied, and your `File` class can be. You even even explicitly invoke the copy-constructor of your class in your initialization of the `file` object (even though it could be elided it must still be valid). – Some programmer dude Mar 13 '21 at 16:56
  • If you want to wrap a stream you need to create it dynamically. – Some programmer dude Mar 13 '21 at 16:59

0 Answers0