【发布时间】:2015-06-16 11:14:05
【问题描述】:
所以我正在尝试在 C++ 中实现文件系统。 我有一个包含地图的目录类。 每个目录都可以在它的地图中保存另一个目录(所以我在地图中有一个地图)
我正在尝试删除一个文件/目录,在它删除的方法中,一切都很好,但是方法完成了我的主地图没有更新......
这里是相关代码:
目录类(使用 Composite 实现):
class Directory : public FileComponent
{
private:
std::map<std::string,FileComponent*> directoryList;
public:
std::map<std::string, FileComponent*> &getMap() {
return directoryList;
}
类文件系统:
private:
FileComponent* find(Mode mode, const std::string& newDirectory, const std::string& directoryName, std::map<std::string, FileComponent*> check);
std::map<std::string,FileComponent*> fileSystem;
};
这就是我试图做的(找到一个文件并复制它......)
void filesys::copy(const std::string& fileName, const std::string& directoryName){
//looking for file
mapitr itr = fileSystem.find(fileName);
if (itr != fileSystem.end()){
fileSystem.erase(itr);
//file found, looikng for directory
}
else{
for (itr = fileSystem.begin(); itr != fileSystem.end(); itr++){
FileComponent* toCopy = find(CopyFile, fileName, directoryName, itr->second->getMap());
if (toCopy != nullptr)
//found! need to copy
break;
}
}
}
还有我的查找方法:
FileComponent* filesys::find(Mode mode,const std::string& newDirectory, const std::string& directoryName, std::map<std::string,FileComponent*> check){
else if (mode == CopyFile){
mapitr itr = check.find(newDirectory);
if (itr != check.end()){
FileComponent* toCopy = itr->second;
check.erase(itr);
return toCopy;
}
else{
for (itr = check.begin(); itr != check.end(); itr++){
FileComponent* toCopy;
try{
toCopy = find(CopyFile, newDirectory, directoryName, itr->second->getMap());
if (toCopy != nullptr){
return toCopy;
}
}
catch (mExceptions& e){
e.what();
}
}
return nullptr;
}
}
}
在它找到并删除的函数内部,一切似乎都井井有条......但是一旦返回目录/文件仍然在我的文件系统中...... 我正在通过引用返回我的内部地图......所以我真的不知道为什么它不想工作......
提前致谢!
【问题讨论】:
标签: c++ dictionary stl