C++ C++;递归地遍历路径。错误:未找到接受类型为';的右操作数的运算符;const std::filesystem::directory#u条目';

C++ C++;递归地遍历路径。错误:未找到接受类型为';的右操作数的运算符;const std::filesystem::directory#u条目';,c++,for-loop,recursion,c++17,C++,For Loop,Recursion,C++17,我想创建一个循环,以迭代方式遍历路径中的所有文件夹和文件。我有以下代码(如上所述) #包括 #包括 for(const auto&dirEntry:std::filesystem::recursive\u directory\u iterator(“此处的某些路径”)){ std::cout标准库中没有运算符的重载标准库中没有运算符的重载您需要定义目录迭代器的打印方式。如果您需要,最容易转换为字符串。请尝试:std::cout My实现(gcc版本10.2.0(第5版,由MSYS2项目构建))为

我想创建一个循环,以迭代方式遍历路径中的所有文件夹和文件。我有以下代码(如上所述)

#包括
#包括
for(const auto&dirEntry:std::filesystem::recursive\u directory\u iterator(“此处的某些路径”)){

std::cout标准库中没有
运算符的重载标准库中没有
运算符的重载您需要定义目录迭代器的打印方式。如果您需要,最容易转换为字符串。请尝试:
std::cout My实现(gcc版本10.2.0(第5版,由MSYS2项目构建))为您的答案定义了一个
运算符库。
std::cout@masteryoda436要将文件名转换为字符串,请使用
字符串()
std::filesystem::path的方法。您需要定义目录迭代器的打印方式。如果需要的话,最容易转换为字符串。请尝试:
std::cout My实现(gcc版本10.2.0(版本5,由MSYS2项目构建))有一个已定义的
operatorTanks供您回答。
std::cout@masteryoda436要将文件名转换为字符串,请使用
std::filesystem::path
string()
方法。谢谢您的回答。这确实有效。但是,我现在想知道为什么我不能转换
dirEntry.path().filename()
到一个字符串,如
std::string file=dirEntry.path().filename()
。不应该
dirEntry.path().filename())
be type string?。错误是
'initialization':无法从'std::filesystem::path'转换为'std::basic_string'
@masteryoda436如果您停止猜测,并查阅您最喜欢的文档以了解如何使用这些函数,您是对的,对不起。对于感兴趣的人:
dirEntry.path().filename().string()
对我有效。谢谢你的回答。这确实有效。但是,我现在想知道为什么我不能将
dirEntry.path().filename()
转换为类似
std::string file=dirEntry.path().filename()
。不应该
dirEntry.path().filename()这样的字符串
be type string?。错误是
'initialization':无法从'std::filesystem::path'转换为'std::basic_string'
@masteryoda436如果您停止猜测,并查阅您最喜欢的文档以了解如何使用这些函数,您是对的,对不起。对于感兴趣的人:
dirEn试试.path().filename().string()
对我有用。
#include <filesystem> 
#include <iostream>

for (const auto& dirEntry : std::filesystem::recursive_directory_iterator("some path here")) {
        std::cout << dirEntry;
    }
'<<': no operator found which takes a right-hand operand of type 'const std::filesystem::directory_entry' (or there is no acceptable conversion)