I'm trying to create a vector of objects of a custom class A:
class A {
std::ifstream _file;
std::string _fileName;
public:
A(std::string &fileName) : _fileName(fileName) {
this->_file = std::ifstream(this->_fileName, std::ios::in);
}
~A() {
this->_file.close();
}
};
In the main I'm pushing n objects of class A with a for loop iterating a vector of file names.
Example:
#include <iostream>
#include <string>
#include <vector>
#include "A.hpp"
int main() {
std::vector<A> AList;
std::vector<std::string> fileList = { "file1", "file2", "file3" };
for (auto &f : fileList) {
std::cout << f << std::endl;
A tempObj(f);
AList.emplace_back(tempObj);
}
return 0;
}
But I get this error:
/usr/include/c++/9.1.0/bits/stl_construct.h:75:7: 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