C++ 如何定义向量<;ifstream>;在c++;班

C++ 如何定义向量<;ifstream>;在c++;班,c++,vector,fstream,C++,Vector,Fstream,我需要一个像这样的向量成员: 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; } A类 { 私人: 矢

我需要一个像这样的向量成员:

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;
}
A类
{
私人:
矢量文件;
公众:
bool addFile(常量字符串和文件路径);
};
bool A::addFile(常量字符串和文件路径)
{
ifstream ifile(filePath.c_str());
_file.push_back(ifile);//但出现错误;
}
我怎样才能成功地完成这门课


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

STL容器需要元素是可复制的可分配的<代码>标准::ifstream不可复制。您需要使用指向
std::ifstream
的智能指针。

如果您有权访问
std::unique\u ptr
,您可以将它们存储在向量中。这不会有危险。你最好使用智能指针来存储它。