如何在<ifstream> C++ 类中定义向量

how to define vector<ifstream> in c++ class

本文关键字:向量 C++ 定义 ifstream lt gt      更新时间:2023-10-16

我需要类中的向量成员,如下所示:

class A
{
private:
vector<ifstream> _files;
public:
bool addFile(const string& filePath);
};
bool A::addFile(const string& filePath)
{
ifstream ifile(filePath.c_str());
_files.push_back(ifile);//but errors;
}

我怎样才能成功地完成这门课;

现在我的解决方案是使用向量。可以吗?还是一些潜在的危险?

STL容器需要元素为CopyConstructibleAssignablestd::ifstream不可复制。您需要使用指向std::ifstream的智能指针。