gpt4 book ai didi

C++ - 没有运算符 "<<"匹配这些操作数 directory_iterator()

转载 作者:太空狗 更新时间:2023-10-29 20:01:45 24 4
gpt4 key购买 nike

我已经在 directory_iterator page 上复制了示例代码,所以这就是我所拥有的:

#include "pch.h" //for visual studios benefit
#include <fstream>
#include <iostream>
#include <filesystem>
namespace fs = std::filesystem;

int main()
{
fs::create_directories("sandbox/a/b");
std::ofstream("sandbox/file1.txt");
std::ofstream("sandbox/file2.txt");
for (auto& p : fs::directory_iterator("sandbox"))
std::cout << p << '\n'; //this line on the first '<<' is where the error occurs
fs::remove_all("sandbox");
return 0; //included by me
}

错误信息是:

Severity    Code    Description Project File    Line    Suppression State
Error (active) E0349 no operator "<<" matches these operands

因为我是 C++ 的新手,所以我可能弄错了,但我对错误的理解基本上是 p 在我的例子中是无法打印到控制台的东西cout.

如果我直接在页面上运行该示例,它就可以工作,所以它没有任何问题,我也不希望出现这种情况。那么问题是,为什么我会看到这个错误?

我使用最新版本的 Visual Studio 以及 C++ 2017。

最佳答案

directory_entry没有直接的流媒体支持;它通过 path 获取的 operator<<加上对 const std::filesystem::path & 的隐式转换.

由于 LWG issues 29893065 , path的运营商已成为隐藏的 friend ,不能再用于流式传输可转换为 path 的内容,例如 directory_entry .

解决方法是请求 .path()直接而不是依赖于隐式转换。我已经修复了 cppreference 示例。


在与 LWG 确认后,此更改似乎是无意的。我已经提交了 LWG 3171 .

关于C++ - 没有运算符 "<<"匹配这些操作数 directory_iterator(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53429574/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com