gpt4 book ai didi

c++ - 将 const unsigned long 流式传输到 fstream

转载 作者:行者123 更新时间:2023-11-28 07:17:46 25 4
gpt4 key购买 nike

我正在用标准类和算法替换一些遗留代码,并替换旧文件类。

obj 有一个无符号长成员 myUL

inline std::fstream& operator <<(std::fstream& rwf, const obj var)
{
rwf << var.myUL;
return rwf;
}

这无法编译,但我的大脑卡住了为什么!错误消息提示它找不到重载“在尝试匹配参数列表'(std::fstream, unsigned long)'时”

我试过了

inline std::fstream& operator <<(std::fstream& rwf, const obj var)
{
unsigned long ul = var.myUL;
rwf << ul;
return rwf;
}

但这也失败了(我认为通过非 const unsigned long 可能会有所帮助,因为 const_casting 它是无效的)。

但是当我在我的测试程序中使用它时,fstream 对 unsigned long 有一个重载。我猜这个问题与类的 const 有关,但是 const_casting 类失败并显示消息:

Conversion requires a constructor or user-defined-conversion operator, which can't be used by const_cast or reinterpret_cast

如果可能的话,我更愿意在不修改该类代码的情况下执行此操作。

针对下面的回答,非常好,答案 - 我已经尝试过同样的方法并添加了#include 行,但它没有帮助。

如果我删除函数中的 const 限定符,它会按预期工作,但我宁愿不这样做。

可以在以下位置看到显示此问题的代码:

Error in motion ...

最佳答案

在测试代码时,它给出了您报告的错误。我添加了#include <fstream>一切正常。

关于c++ - 将 const unsigned long 流式传输到 fstream,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19959022/

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