samedi 21 septembre 2019

C++ vector of objects of custom class - copy constructor deleted - std::ifstream

I'm trying to create a vector of objects of class A.

    class A {
        std::ifstream _file;
        std::string _fileName;

    public:
        A(std::string &fileName);
    };

Constructor:

A::A(std::string &fileName) :
        _fileName(fileName) {
               this->_file = std::ifstream(this->_fileName, std::ios::in);
}

In the main I'm pushing n objects of class A with a for loop iterating a vector of file names.

std::vector<A> AList;
std::vector<std::string> fileList; // populated later
...
     for (const auto &f : fileList) {
                A tempFile(f);
                filesList.emplace_back(tempFile);
            }

But I get this error: /usr/include/c++/9.1.0/ext/new_allocator.h:145:20: error: use of deleted function ‘A::A(const A&)’

If I'm not wrong, since I have a member std::ifstream inside my class A, the copy constructor is deleted (Ref: https://en.cppreference.com/w/cpp/io/basic_ifstream/basic_ifstream)

How can I solve it? What I'm doing wrong?

Thanks for your help

Aucun commentaire:

Enregistrer un commentaire