如何将 std::ofstream& 传递给功能参数?
Posted
技术标签:
【中文标题】如何将 std::ofstream& 传递给功能参数?【英文标题】:How to pass std::ofstream& into functional params? 【发布时间】:2017-08-10 07:50:20 【问题描述】:这是我的代码
void timer_start(std::function<void(std::ifstream*,std::ofstream&, char*[])> func, unsigned int interval, std::ifstream* ifs, std::ofstream& ofs, char* argv[])
std::thread([func, interval, ifs, ofs, argv]()
while (true)
auto x = std::chrono::steady_clock::now() + std::chrono::milliseconds(interval);
func(ifs, (std::ofstream&)ofs, argv);
std::this_thread::sleep_until(x);
).join();
上述代码第二行在传递ofs
时出现如下错误错误 C2280 'std::basic_ofstream>::basic_ofstream(const std::basic_ofstream> &)':试图引用已删除的函数
为什么会发生这种情况,请帮助我..
【问题讨论】:
你try the solution offered in this question. Passing an ofstream to a thread function的可能重复 【参考方案1】:你应该通过引用而不是值来捕获ofs
:
std::thread([func, interval, ifs, &ofs, argv]()
^^^^^^
here
并可能删除不必要的演员表:(std::ofstream&)ofs
。
DEMO
【讨论】:
C2661 'std::tuple将std::thread([func, interval, ifs, &ofs, argv]()
更改为std::thread([func, interval, ifs, &ofs, argv]()
。
关于ofstream你可以看here
也许你需要知道关键字 delete and default 在 c++11 中的含义
【讨论】:
以上是关于如何将 std::ofstream& 传递给功能参数?的主要内容,如果未能解决你的问题,请参考以下文章
通过 SWIG 在 python 中使用 std::ifstream、std::ofstream 的技术?
为啥我可以在临时 std::ofstream 对象上使用 `operator<<`?
std::ofstream 无法在 win7/64 和 msvc2013 上使用 std::ios::ate 打开大文件