錯誤:的std :: for_each的函子+難以理解給出錯誤
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,而不是字符串&的。我發現我試圖解引用一個迭代器,或直接發送一個字符串到運算符()。
我知道並不是每個聲明/定義都顯示在這裏,但希望有足夠的信息以便有人能夠發現我的錯誤。我現在已經失明瞭。多謝。
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);
}
UPDATE 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;
你總是可以嘗試一個普通的'for'循環。我喜歡他們。沒有不可理解的恐怖消息。 –
@ Cheersandhth.-Alf:這實際上是一個很好的答案,尤其是考慮到問題的原因。它使我的代碼編譯。當然,我正在傳遞迭代器,這正是我以前認爲的for_each所做的。 – Chap