C++ 中的高阶函数使用 std::wcout 失败并出现错误 C2248
我正在尝试用 C++ 实现函数式的东西。目前,我正在研究用于枚举文件的连续传递样式。
我有一些代码如下所示:
namespace directory
{
void find_files(
std::wstring &path,
std::function<void (std::wstring)> process)
{
boost::filesystem::directory_iterator begin(path);
boost::filesystem::directory_iterator end;
std::for_each(begin, end, process);
}
}
然后我这样调用它:
directory::find_files(source_root, display_file_details(std::wcout));
...其中 display_file_details
定义如下:
std::function<void (std::wstring)>
display_file_details(std::wostream out)
{
return [&out] (std::wstring path) { out << path << std::endl; };
}
计划是将延续传递给 find_files< /code>,但能够将组合函数传递给它。
但我收到错误:
error C2248: 'std::basic_ios<_Elem,_Traits>::basic_ios' :
cannot access private member declared in
class 'std::basic_ios<_Elem,_Traits>'
我做错了什么?我尝试这个是疯了吗?
注意:我的函数术语(高阶、延续等)可能是错误的。请随时纠正我。
I'm playing around with implementing functional-style stuff in C++. At the moment, I'm looking at a continuation-passing style for enumerating files.
I've got some code that looks like this:
namespace directory
{
void find_files(
std::wstring &path,
std::function<void (std::wstring)> process)
{
boost::filesystem::directory_iterator begin(path);
boost::filesystem::directory_iterator end;
std::for_each(begin, end, process);
}
}
Then I'm calling it like this:
directory::find_files(source_root, display_file_details(std::wcout));
...where display_file_details
is defined like this:
std::function<void (std::wstring)>
display_file_details(std::wostream out)
{
return [&out] (std::wstring path) { out << path << std::endl; };
}
The plan is to pass a continuation to find_files
, but to be able to pass composed functions into it.
But I get the error:
error C2248: 'std::basic_ios<_Elem,_Traits>::basic_ios' :
cannot access private member declared in
class 'std::basic_ios<_Elem,_Traits>'
What am I doing wrong? Am I insane for trying this?
Note: my functional terminology (higher-order, continuations, etc.) is probably wrong. Feel free to correct me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在
display_file_details
中,您需要通过引用获取您的 wostream。 iostream 复制构造函数是私有的。In
display_file_details
, you need to take your wostream by reference. iostream copy constructors are private.在更深入地查看编译器输出后,我发现了这一点:
事实证明 basic_ostream 没有可用的复制构造函数。
将 std::wostream out 更改为 std::wostream & out 修复了它。至少到了我收到一堆其他错误的程度。这些很容易通过以下方式修复:
Upon looking more deeply into the compiler output, I found this:
It turns out that basic_ostream doesn't have an available copy constructor.
Changing
std::wostream out
tostd::wostream & out
fixes it. At least to the point that I get a bunch of other errors. These were easily fixed by: