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));

编译(g++ -O3 -std=c++11 -fopenmp -fPIC -Wall -g -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> >’
4

2 回答 2

1

DocID 类型是否可散列?这对于 unordered_map 的键类型是必需的,这就是编译器所抱怨的。另一方面,DocID 听起来像是支持订单,为什么不使用映射,它被实现为二叉搜索树呢?

此外,您的复制构造函数和复制赋值运算符已损坏。他们复制指针,然后原始对象和副本都认为他们拥有资源。如果其中一个被破坏,则资源被删除,当另一个对象被破坏时导致双重释放。

于 2013-04-04T09:09:10.943 回答
1

有很多缺失的信息使我们无法为您提供正确的答案。例如,什么是doc_bk_hamming?您分配并将unordered_map其地址分配给指针my_map。但是,您将该对插入到我们看不到(my_key, my_value)的指向的地图中。doc_bk_hamming

于 2013-04-04T09:30:36.967 回答