无序映射:自己的值类型问题
Unordered Map: Own Value Type Issue
我有一个用户定义的类,它应该是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));
编译(c++ -O3 -std=c++11 -fopenmp -fPIC -Wall -g -I)-我。/include -c -o ref_impl/core.o) Error:
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> >’
DocID类型可哈希吗?这对于unordered_map的键类型是必要的,这就是编译器抱怨的地方。另一方面,DocID听起来像是支持顺序的,为什么不使用map呢?它是作为二叉搜索树实现的。
同样,您的复制构造函数和复制赋值操作符也失效了。它们复制指针,然后原始对象和复制对象都认为自己拥有资源。如果其中一个对象被销毁,资源将被删除,当另一个对象被销毁时,将导致双重释放。
有很多遗漏的信息,使我们无法给你一个正确的答案。例如,doc_bk_hamming
是什么?分配和unordered_map
,并将其地址分配给指针my_map
。但是,您正在将(my_key, my_value)
对插入到我们看不到的doc_bk_hamming
指向的映射中。
相关文章:
- C++ 雷神库 - 使用资源加载器类时出现问题(不命名类型)
- C++LinkedList问题.数据类型之间存在冲突?没有匹配的构造函数
- 指针问题:从不兼容的类型"int"分配给"int *"
- 特征返回类型的 pybind11 问题
- 对齐C++字符串类型问题 std::字符串到 TStr
- C 中的类型问题
- dllimport类型问题
- 变量类型问题
- 并排定义指针和类型问题
- C++对数据类型问题
- B-树搜索引用子数据类型问题
- 协议缓冲区,让C#与C++对话:类型问题和模式问题
- 传递类成员函数:类型问题
- 使用受限函数时,尾随返回类型问题
- 如何正确使用模板?我在使用模板时遇到数据类型问题<>
- 链表输入类型问题
- 无序映射:自己的值类型问题
- 将windows c++项目链接到boost命令行体系结构类型问题
- 有数据类型问题的c++阶乘函数
- C++返回类型问题