2013-04-04 53 views
0

我有一個用戶定義的類,它應該是unsorted_map的值類型。我的問題是,插入不工作(用gcc 4.7編譯時已經是一個靜態錯誤)。無序圖:自有值類型問題

我的類:以創建地圖並插入

class bk_tree { 

public: 
    bk_point *root; 
    DISTANCE_FUN metric; 
    int max_depth; 

    //assume words not empty 
    bk_tree() {metric=NULL; root=NULL; max_depth = 0;} 

    //rule of three 
    bk_tree(const bk_tree& copy_this) {metric=copy_this.metric; root=copy_this.root; max_depth = copy_this.max_depth;} 
    bk_tree& operator=(const bk_tree& copy_this) { metric=copy_this.metric; root=copy_this.root; max_depth = copy_this.max_depth; return *this; } 
    ~bk_tree() { delete root; } 

    bk_tree(unordered_set<string> *words, DISTANCE_FUN _metric); 
    bk_tree(DISTANCE_FUN _metric) { metric = _metric; root = NULL; max_depth = 0;   
}; 

代碼:

#include<tr/unordered_map> 
using namespace std; 
using namespace std::tr1; 

unordered_map<DocID, sigmod::bk_tree> *my_map = new unordered_map<DocID, sigmod::bk_tree>; 
sigmod::bk_tree my_value = sigmod::bk_tree(&words, sigmod::hamming_distance_metric); 
doc_bk_hamming->insert(make_pair(my_key, my_value)); 

彙編(克++ -O3 -std = C++ 11 -fopenmp -fPIC -Wall - 克-I -I./include -c -o ref_impl/core.o)錯誤:

ref_impl/core.cpp 
In file included from /usr/include/c++/4.7/bits/move.h:57:0, 
       from /usr/include/c++/4.7/bits/stl_pair.h:61, 
       from /usr/include/c++/4.7/bits/stl_algobase.h:65, 
       from /usr/include/c++/4.7/bits/char_traits.h:41, 
       from /usr/include/c++/4.7/string:42, 
       from ref_impl/../include/metric.h:5, 
       from ref_impl/core.cpp:29: 
/usr/include/c++/4.7/type_traits: In instantiation of ‘struct std::is_convertible<const std::tr1::__detail::_Hashtable_iterator<std::pair<const unsigned int, sigmod::bk_tree>, false, false>&, std::tr1::__detail::_Hashtable_iterator<std::pair<const unsigned int, sigmod::bk_tree>, false, false> >’: 
/usr/include/c++/4.7/type_traits:116:12: required from ‘struct std::__and_<std::is_convertible<const std::tr1::__detail::_Hashtable_iterator<std::pair<const unsigned int, sigmod::bk_tree>, false, false>&, std::tr1::__detail::_Hashtable_iterator<std::pair<const unsigned int, sigmod::bk_tree>, false, false> >, std::is_convertible<const bool&, bool> >’ 
/usr/include/c++/4.7/bits/stl_pair.h:113:38: required from here 
/usr/include/c++/4.7/type_traits:1263:12: error: the value of ‘std::__is_convertible_helper<const std::tr1::__detail::_Hashtable_iterator<std::pair<const unsigned int, sigmod::bk_tree>, false, false>&, std::tr1::__detail::_Hashtable_iterator<std::pair<const unsigned int, sigmod::bk_tree>, false, false>, false>::value’ is not usable in a constant expression 
/usr/include/c++/4.7/type_traits:1258:70: note: ‘std::__is_convertible_helper<const std::tr1::__detail::_Hashtable_iterator<std::pair<const unsigned int, sigmod::bk_tree>, false, false>&, std::tr1::__detail::_Hashtable_iterator<std::pair<const unsigned int, sigmod::bk_tree>, false, false>, false>::value’ used in its own initializer 
/usr/include/c++/4.7/type_traits:1263:12: note: in template argument for type ‘bool’ 
/usr/include/c++/4.7/type_traits: In instantiation of ‘struct std::__and_<std::is_convertible<const std::tr1::__detail::_Hashtable_iterator<std::pair<const unsigned int, sigmod::bk_tree>, false, false>&, std::tr1::__detail::_Hashtable_iterator<std::pair<const unsigned int, sigmod::bk_tree>, false, false> >, std::is_convertible<const bool&, bool> >’: 
/usr/include/c++/4.7/bits/stl_pair.h:113:38: required from here 
/usr/include/c++/4.7/type_traits:116:12: error: ‘value’ is not a member of ‘std::is_convertible<const std::tr1::__detail::_Hashtable_iterator<std::pair<const unsigned int, sigmod::bk_tree>, false, false>&, std::tr1::__detail::_Hashtable_iterator<std::pair<const unsigned int, sigmod::bk_tree>, false, false> >’ 
+0

'root = copy_this.root;'您正在複製指針的值。你知道嗎? – 2013-04-04 09:02:11

+0

您的副本構造函數和複製指派操作符已損壞。操作之後,兩個對象將具有相同的指針,並且兩個析構函數都會嘗試刪除它。 – 2013-04-04 09:03:28

+0

什麼是DocID? – ForEveR 2013-04-04 09:05:09

回答

1

是對的DocID類型可哈希?這對於unordered_map的鍵類型是必需的,這是compailer所抱怨的。另一方面,DocID聽起來像支持一個命令,爲什麼不使用一個映射,它被實現爲二叉搜索樹?

此外,您的副本構造函數和複製賦值運算符已損壞。他們複製指針,然後原始對象和副本都認爲他們擁有資源。如果其中一個被破壞,則該資源被刪除,導致在另一個對象被破壞時產生雙重空閒。

+0

typedef unsigned int DocID。所以它應該是可散列的?我修復了析構函數,錯誤仍然:( – mateuszk87 2013-04-04 09:14:24

1

有很多遺漏的信息,阻止我們給你一個正確的答案。例如,什麼是doc_bk_hamming?您分配和unordered_map並將其地址分配給指針my_map。但是,您將(my_key, my_value)對插入我們看不到的doc_bk_hamming所指的地圖中。