这是我的问题,我需要创建 X 个文件并根据不同的事实写入它们,我的解决方案是创建一个像这样的 ofstream 指针向量
#include <boost/algorithm/string.hpp>
#include <vector>
#include<string>
#include <iostream>
#include <fstream>
vector<ofstream*> files;
files.resize(SplitVec.size()-4);
for(i=0;i<SplitVec.size()-4;i++)
{
line="/Users/jorge/Desktop/testing/strain_"+lexical_cast<string>(i);
cout<<"ine"<<endl;
files[i]=new ofstream(line.c_str());
}
直到这部分它创建了很好的文件,然后在我写给它们的程序中,这也很好,我的问题是当我想关闭对象时,如果我使用:
for(i=0;i<SplitVec.size()-4;i++)
{
*files[i].close();
}
我收到以下错误:
In file included from main.cpp:14:./methyl.h:298: error: request for member 'close' in 'files. std::vector<_Tp, _Alloc>::operator[] [with _Tp = std::ofstream*, _Alloc = std::allocator<std::ofstream*>](1ul)', which is of non-class type 'std::ofstream*'
所以,我有一个问题,首先,为什么我不能调用 close,它是一个指向 ofstream 的指针,所以使用 *files[i],我会想象我可以关闭它,其次,如果我不关闭它,程序工作正常,但我几乎可以肯定这是一种不好的做法,并且不想成为一个懒惰或蹩脚的程序员,我已经尽可能多地看了,但我找不到答案。谢谢!!!