C++ std::每个+;给出不可理解错误的函子

C++ std::每个+;给出不可理解错误的函子,c++,foreach,functor,C++,Foreach,Functor,错误: g++ -c -g -O0 -fno-strict-aliasing --std=c++11 -I/home/chap/private/WDI/git -I/home/chap/private/WDI/git/include -I/usr/include/mysql -fno-strict-aliasing tom.cpp In file included from /usr/include/c++/4.7/algorithm:63:0, from

错误:

g++ -c -g -O0 -fno-strict-aliasing  --std=c++11 -I/home/chap/private/WDI/git -I/home/chap/private/WDI/git/include -I/usr/include/mysql -fno-strict-aliasing  tom.cpp
In file included from /usr/include/c++/4.7/algorithm:63:0,
                 from /usr/include/boost/optional/optional.hpp:19,
                 from /usr/include/boost/optional.hpp:15,
                 from /usr/include/boost/format/internals.hpp:20,
                 from /usr/include/boost/format.hpp:38,
                 from tom.h:16,
                 from tom.cpp:1:
/usr/include/c++/4.7/bits/stl_algo.h: In instantiation of ‘_Funct std::for_each(_IIter, _IIter, _Funct) [with _IIter = __gnu_cxx::__normal_iterator<std::basic_string<char>*, std::vector<std::basic_string<char> > >; _Funct = bind_from_memory]’:
/home/chap/private/WDI/git/Block.h:227:2:   required from ‘void Block::populate_output_record(const D_RecordType&, Function) [with Function = bind_from_memory; D_RecordType = std::basic_string<char>]’
tom.cpp:901:8:   required from here
/usr/include/c++/4.7/bits/stl_algo.h:4442:2: error: no match for call to ‘(bind_from_memory) (std::basic_string<char>&)’
tom.cpp:864:8: note: candidate is:
tom.cpp:871:10: note: void bind_from_memory::operator()(M_Field_Iter)
tom.cpp:871:10: note:   no known conversion for argument 1 from ‘std::basic_string<char>’ to ‘M_Field_Iter {aka __gnu_cxx::__normal_iterator<std::basic_string<char>*, std::vector<std::basic_string<char> > >}’
make: *** [tom.o] Error 1
其中b是函子。“实例化”内容提到了每个的
。那么,这应该是在函子上启动迭代器,而不是字符串&。我看不到我正在尝试取消对迭代器的引用,或者直接将字符串发送到操作符()

我知道并不是每个声明/定义都显示在这里,但希望有足够的声明/定义,以便有人能够发现我的错误。我现在已经瞎了。非常感谢

汤姆·卡普

// ============================================================================
//
// This is a functor that gets applied as we traverse the fieldname
// vector.  It uses fieldname to retrieve a value from memory, and
// push_back()s the value onto the value vector.
struct bind_from_memory {
    // CTOR - bind object to MemoryBank and to MemRef_Vec
    bind_from_memory(MemoryBank& memory, D_MemRef_Vec* memrefs_ptr) : 
    memory_map(memory),
    memref_vec(*memrefs_ptr)  {};

    // Overload of (), enabling calls to 
    // bind_from_memory(M_Field_Iter)
    void operator()(M_Field_Iter field_iter) {  //  <<------Line 871----------------
    MemoryBank::iterator i;
    if ( ( i = memory_map.find(*field_iter)) != memory_map.end() ) {
        memref_vec.emplace_back(i->second);
    }
    else {
        memref_vec.emplace_back(); // not found, default ""
    }
    }
private:
    // bound for the duration of a single populate_output_record
    MemoryBank&   memory_map; // we retrieve values from here
    D_MemRef_Vec& memref_vec; // we push_back the values onto this
};

// ----------------------------------------------------------------------------
// bind_output_record(memory, block, record)
// 
// Fill out an output record by pulling its field values from memory.
//
// Initialize a zero-length record vector, and then invoke populate_output_record
// to traverse the fieldnames, invoking bind_from_memory on every iteration.
// bind_from_memory(
//
void
bind_output_record(MemoryBank& memory, Block &block, const D_RecordType &rec_type) {
    block.add_record(rec_type); // create record stub
    bind_from_memory b(memory, 
               block.get_output_record_ptr(rec_type));
    block.populate_output_record(rec_type, 
                 b ); //  <<--- Line 901 ----------------------------
}
街区h

// typedef pair<M_Field_Iter, M_Field_Iter>  M_Field_Range;

M_Field_Range           make_field_range(const D_RecordType&);

template <typename Function>
void
populate_output_record(const D_RecordType& record_type, 
               Function func) {
    M_Field_Range fields = make_field_range(record_type);

    std::for_each(fields.first, fields.second, func);
}
//typedef对M_字段_范围;
M_Field_Range make_Field_Range(const D_RecordType&);
模板
无效的
填充输出记录(常数D记录类型和记录类型,
函数(func){
M_Field_Range fields=make_Field_Range(记录类型);
std::for_each(fields.first,fields.second,func);
}
更新1:M_Record_Hash和associates,根据要求

typedef string                            M_FieldName;
typedef int                               M_FieldPosition;
typedef string                            M_RecordType;
typedef bool                              M_RecordMandatory;
typedef char                              M_BlockFunction;
typedef string                            M_BlockType;

typedef vector<M_FieldName>               M_Field_Vec;
typedef boost::unordered_map<M_FieldName, M_FieldPosition>
                                          M_FieldIndex_Hash;
typedef pair<M_FieldIndex_Hash, M_Field_Vec> 
                                          M_FieldInfo_Pair;

typedef boost::unordered_map<M_RecordType, M_FieldInfo_Pair> 
                                          M_Record_Hash;
typedef字符串M_字段名;
类型定义int M_字段位置;
typedef字符串M_记录类型;
typedef bool mu记录必填;
typedef char mu块函数;
typedef字符串M_BlockType;
typedef向量M_Field_Vec;
typedef boost::无序_映射
M_FieldIndex_Hash;
typedef对
M_FieldInfo_配对;
typedef boost::无序_映射
M_记录_散列;

对于每个_
都不会将函子传递给迭代器,它会传递来自取消引用迭代器的任何函子

__gnu_cxx::__normal_iterator<std::basic_string<char>*, std::vector<std::basic_string<char> > >
\uuuu gnu cxx::\uu normal\u迭代器

表示您正在迭代
向量
。为此,您的函子必须接受与
string
(即
string
string&
const string&
)兼容的类型,而不是它当前正在接受的迭代器。

您始终可以尝试普通的
for
循环。我喜欢它们。没有不可理解的厄洛尔梅萨日。@Cheersandhth.-Alf:这实际上是一个很好的答案,特别是考虑到问题的结果。它使我的代码得以编译。当然,我传递的是迭代器,这是我认为每个人都在做的事情。这似乎是事实
for_each
不会将其迭代器传递给函子,而是首先取消对其迭代器的引用。这是正确的答案。
typedef string                            M_FieldName;
typedef int                               M_FieldPosition;
typedef string                            M_RecordType;
typedef bool                              M_RecordMandatory;
typedef char                              M_BlockFunction;
typedef string                            M_BlockType;

typedef vector<M_FieldName>               M_Field_Vec;
typedef boost::unordered_map<M_FieldName, M_FieldPosition>
                                          M_FieldIndex_Hash;
typedef pair<M_FieldIndex_Hash, M_Field_Vec> 
                                          M_FieldInfo_Pair;

typedef boost::unordered_map<M_RecordType, M_FieldInfo_Pair> 
                                          M_Record_Hash;
__gnu_cxx::__normal_iterator<std::basic_string<char>*, std::vector<std::basic_string<char> > >