2011-09-29 103 views
1

我在C++中实现函数式的东西。目前,我正在寻找一种继续传递文件的方式来枚举文件。使用std :: wcout的C++中的高阶函数失败,出现错误C2248

我有一些代码,看起来像这样:

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,但能够通过COM向它提出了功能。

但我得到的错误:

error C2248: 'std::basic_ios<_Elem,_Traits>::basic_ios' : 
    cannot access private member declared in 
    class 'std::basic_ios<_Elem,_Traits>' 

我在做什么错?我为此尝试疯了吗?

注:我的功能术语(高阶,延续等)可能是错误的。随时纠正我。

+1

您无法通过值传递wostream。仅供参考。 'std :: wostream&out' –

+0

为什么'display_file_details'是一个生成器而不是实际的函数? –

+0

因为我在玩耍,这就是我在功能风格的C#或F#中所做的。如果我在考虑C++ 03,我想可以使用'bind'。 –

回答

5

display_file_details,你需要把你的wostream作为参考。 iostream拷贝构造函数是私有的。

4

在寻找更多的深入到编译器的输出,我发现这一点:

This diagnostic occurred in the compiler generated function 
    'std::basic_ostream<_Elem,_Traits>:: 
     basic_ostream(const std::basic_ostream<_Elem,_Traits> &)' 

事实证明,basic_ostream没有可用的拷贝构造函数。

std::wostream out更改为std::wostream & out修复它。至少到了我得到一堆其他错误的时候。这些很容易通过以下方式修复:

std::for_each(begin, end, 
    [&process] (boost::filesystem::directory_entry d) 
    { process(d.path().wstring()); });