字典定義爲如下:作爲GUID的std ::地圖鍵
typedef boost::tuple<conn_ptr, handler_ptr, rdp_ptr> conn_tuple;
typedef std::map<GUID, conn_tuple> conn_map;
我們得到了一個編譯錯誤:
Error 9 error C2678: binary '<' : no operator found which takes a left-hand operand of type 'const GUID' (or there is no acceptable conversion) c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstddef
然後我們解決它:
struct GUIDComparer
{
bool operator()(const GUID & Left, const GUID & Right) const
{
// comparison logic goes here
if((Left.Data1 == Right.Data1) && (Left.Data2 == Right.Data2) &&
(Left.Data3 == Right.Data3) && (memcmp(Left.Data4 , Right.Data4,sizeof(Right.Data4))==0) )
{
return true;
}
return false;
}
};
typedef boost::tuple<conn_ptr, handler_ptr, rdp_ptr> conn_tuple;
typedef std::map<GUID, conn_tuple, GUIDComparer> conn_map;
現在全部編譯完成,但是在運行時我們得到一個異常(無效的運算符<)。
我不知道什麼是錯的,會很高興,如果有人可以幫助
另外,請注意'<'是「小於」,而不是「等於」。因此,當memcmp()返回小於零的值時返回'true'。你實現了錯誤的謂詞,這就是你得到異常的原因。 – xaizek 2015-04-03 17:26:42
@feras請參閱修訂後的答案。前一個有一個重要的邏輯錯誤。 – 2015-04-03 17:55:50