std :: for_each + functor给出了难以理解的错误

时间:2013-12-10 04:29:02

标签: 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 /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

我找不到它。对我而言,这表示有人希望将string&传递给bind_from_memory::operator()(M_Field_Iter),而“某人”可能会因此而产生:

Line 901: block.populate_output_record(rec_type, b );

其中b是仿函数。 “实例化”内容提到了for_each。好吧,那应该是在函子中启动ITERATORS,而不是string&amp; s。我看到NOWHERE我试图取消引用迭代器,或直接将字符串发送到operator()。

我知道并非每个声明/定义都在这里显示,但希望有足够的人可能会发现我的错误。我现在已经失明了。很有责任。

tom.cpp

// ============================================================================
//
// 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 ----------------------------
}

Block.cpp

M_Field_Range
Block::make_field_range(const D_RecordType& record_type) {

    // point to fieldname vector
    M_Field_Vec& fields = m_record_hash->at(record_type).second;

    return std::make_pair(fields.begin(),
              fields.end()
              );
}

Block.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);
}

更新1 :M_Record_Hash并按要求关联

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;

1 个答案:

答案 0 :(得分:2)

for_each不会将函数传递给迭代器,它会传递函子来解除迭代器的引用。

__gnu_cxx::__normal_iterator<std::basic_string<char>*, std::vector<std::basic_string<char> > >

表示您正在迭代vector<string>。为此,您的仿函数必须接受与string兼容的类型(即stringstring&const string&),而不是它当前正在执行的迭代器。