2012-07-05 63 views
1

我正在嘗試創建一個動態屬性映射。爲此,我創建了單獨的屬性映射Dynamic_attribute,然後創建了一個std :: unordered_map來存儲它們的容器。下面是我寫的代碼。但在編譯時使用g ++選項-std = C++ 0x會導致一個非常奇怪的錯誤。使用std :: pair作爲std :: unordered_map中的鍵

#include <iostream> 
#include <vector> 
#include <map> 
#include <unordered_map> 
#include <algorithm> 
#include <string> 
#include <typeinfo> 
#include <cstdlib> 
#include <cstring> 
#include <boost/property_map/property_map.hpp> 
#include <boost/property_map/dynamic_property_map.hpp> 

class Dynamic_attribute_base{ 
public: 
    Dynamic_attribute_base(const std::string& name) : name_(name){} 

    /// Destructor 
    virtual ~Dynamic_attribute_base(){} 

    /// Destroy the object 
    virtual void destroy() = 0; 

protected: 
    std::string name_; 
}; 

/// @models 
/// - LValuePropertyMap 
/// - std:unordered_map 
template<typename Key, typename Value> 
class Dynamic_attribute 
    : public Dynamic_attribute_base{ 
public: 
    typedef Key key_type; 
    typedef Value value_type; 
    typedef std::unordered_map<key_type, value_type> map_type; 
    typedef typename map_type::reference referencnce; 
    typedef typename map_type::const_reference const_reference; 

    /// Constructor 
    Dynamic_attribute(const std::string& name): 
    Dynamic_attribute_base(name) {} 

public: // virtual interface of Dynamic_attribute_base 

    virtual void destroy(){ 
    data_.clear(); 
    this->~Dynamic_attribute(); 
    } 
public: 
    /// Return the value corresponding to key k. 
    /// if key doesn't exists unexpected value is returned. 
    value_type get(key_type& k){ 
    assert(!data_.empty()); 
    if (data_.find(k) != data_.end()){ 
     return data_[k]; 
    } 
    else{ 
     std::cout<<"Key not found"<< std::endl; 
     return NULL; 
    } 
    } 

    /// Set the property corresponding to particular Key K 
    /// to value V. 
    void set(key_type& k, value_type& v){ 
    data_[k] = v; 
    } 

    /// Remove a key from the map. 
    void remove(key_type& k){ 
    data_.erase(k); 
    } 

protected: 
    map_type data_; 
}; 

std::unordered_map< std::pair<std::string,unsigned int> , Dynamic_attribute_base*> d_attribute_container; 
template<typename T> 
void add_dynamic_attribute(unsigned int admin, std::string name){ 
    if (d_attribute_container.find(make_pair(name,admin)) != d_attribute_container.end()){ 
    std::cout<<"Error. Key already exists."; 
    return; 
    } 
    Dynamic_attribute<int, T> *d_attr = new Dynamic_attribute<int, T>(name); 
    d_attribute_container[make_pair(name,admin)] = d_attr; 
} 

template<typename T> 
T get_dart_attribute(int handle, unsigned int admin, std::string name){ 
    if (d_attribute_container.find(make_pair(name,admin)) == d_attribute_container.end()){ 
     std::cout<<"Error. Key doesn't exists."; 
     return; 
    } 
    return dynamic_cast<Dynamic_attribute<int,T>*>(d_attribute_container[make_pair(name,admin)])->get(handle); 
} 

int main() 
{ 
    add_dynamic_attribute<std::string>(1,"first"); 
    add_dynamic_attribute<double>(2,"second"); 
    add_dynamic_attribute<double>(2,"second"); 
    return 0; 
} 

錯誤消息/tmp/ccLK64Ja.o:在功能std::__detail::_Hash_code_base<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, unsigned int>, std::pair<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, unsigned int> const, Dynamic_attribute_base*>, std::_Select1st<std::pair<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, unsigned int> const, Dynamic_attribute_base*> >, std::equal_to<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, unsigned int> >, std::hash<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, unsigned int> >, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, false>::_M_hash_code(std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, unsigned int> const&) const': main.cpp:(.text._ZNKSt8__detail15_Hash_code_baseISt4pairISsjES1_IKS2_P22Dynamic_attribute_baseESt10_Select1stIS6_ESt8equal_toIS2_ESt4hashIS2_ENS_18_Mod_range_hashingENS_20_Default_ranged_hashELb0EE12_M_hash_codeERS3_[std::__detail::_Hash_code_base<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, unsigned int>, std::pair<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, unsigned int> const, Dynamic_attribute_base*>, std::_Select1st<std::pair<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, unsigned int> const, Dynamic_attribute_base*> >, std::equal_to<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, unsigned int> >, std::hash<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, unsigned int> >, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, false>::_M_hash_code(std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, unsigned int> const&) const]+0x37): undefined reference to的std ::散列,性病::分配器>,無符號整型>> ::運算符()(的std :: pair,std :: allocator>,unsigned int>)const'

任何想法?

+1

它看起來像你缺少你的配對哈希函數。看看[這個答案](http://stackoverflow.com/a/2099590/335858)如何創建它。 – dasblinkenlight 2012-07-05 10:38:23

+0

哦,你應該真正地將#include的數量減到最少。 – 2012-07-05 11:59:04

+0

看看這個鏈接:http://stackoverflow.com/questions/919612/mapping-two-integers-to-one-in-a-unique-and-deterministic-way – user2029426 2017-02-09 22:08:42

回答

1

您需要爲您的配對專門設置std::hash,或者爲其提供散列模板參數。

相關問題