所以这就是发生的事情。我正在尝试创建这个小功能,我几乎只是从这个名为 stackoverflow.com 的美妙地方复制粘贴过来的。我唯一添加的是其中的 if 语句来搜索具有特定结尾的字符串(在我的例子中是'.kbin')。一旦找到该文件,它应该将其添加到向量列表事物的末尾。
#include <string>
#include <iostream>
#include <vector>
#include <filesystem>
namespace fs = std::filesystem;
void GetListOfFiles(std::string path, std::vector<std::string>* files)
{
for (const auto& entry : fs::recursive_directory_iterator(path)) {
if (entry.subentry(entry.find_last_of(".") + 1) == "kbin")
&files->push_back(entry.path());
}
}
int main(int argc, char** argv)
{
std::string path = argv[1];
std::vector<std::string> files;
GetListOfFiles(path, files);
}
当我尝试编译它时会出现问题。也许我只是做错了什么,但无论出于何种原因,编译器都会这样说:
- 错误:'const class std::filesystem::__cxx11::directory_entry' 没有名为 'substr' 的成员 15 | if(entry.substr(entry.find_last_of(".") + 1) == "kbin")
- 错误:'const class std::filesystem::__cxx11::directory_entry' 没有名为 'find_last_of' 的成员 15 | if(entry.substr(entry.find_last_of(".") + 1) == "kbin")
- 错误:需要左值作为一元“&” 操作数 16 | &files->push_back(entry.path());
请帮忙。我正在使用 g++ 版本 9.3.0 并正在使用下一个命令进行编译g++ main.cpp -o main -lstdc++fs -std=c++17。