2012-01-28 7 views
6

últimamente estoy más un usuario del lenguaje de programación Scala que C++, Y ahora me siento frustrado en portar una línea muy simple de códigoGLM :: ivec2 como clave en el mapa desordenada

val map = new HashMap[Vec2i,Entity] 

que simplemente se niega a compila en C++ con extraños errores de plantilla. El equivalente de Vec2i en C++ es glm :: ivec2, básicamente es una estructura con dos enteros y algunos operadores de matemática.

esto es hasta qué punto tengo:

#include <iostream> 
#include <unordered_map> 
#include <glm/glm.hpp> 

using namespace std; 
using namespace glm; 

struct KeyTraits { 
size_t operator()(const ivec2& k) { 
     return std::hash<int>()(k.x)^std::hash<int>()(k.y); 
    } 

bool operator()(const ivec2& a, const ivec2& b) { 
     return a.x == b.x && a.y == b.y; 
    } 
}; 

typedef unordered_map<ivec2,int,KeyTraits,KeyTraits> MyMap; 

int main(int argc, char **argv) 
{ 
    MyMap map; 

    map[ivec2(2,3)] = 7; 
    map[ivec2(3,4)] = 8; 

    for(auto it = map.begin(); it != map.end(); it++) { 
     cout << it->second << endl; 
    } 
} 

El error de compilación actual es:

In file included from /usr/include/c++/4.5/bits/hashtable.h:35:0, 
      from /usr/include/c++/4.5/unordered_map:45, 
      from /home/arne/codelite/LanguageTest/main.cpp:2: 
/usr/include/c++/4.5/bits/hashtable_policy.h: In member function ‘std::__detail::_Hash_code_base<_Key, _Value, _ExtractKey, _Equal, _H1, _H2, std::__detail::_Default_ranged_hash, false>::_Hash_code_type std::__detail::_Hash_code_base<_Key, _Value, _ExtractKey, _Equal, _H1, _H2, std::__detail::_Default_ranged_hash, false>::_M_hash_code(const _Key&) const [with _Key = glm::detail::tvec2<int>, _Value = std::pair<const glm::detail::tvec2<int>, int>, _ExtractKey = std::_Select1st<std::pair<const glm::detail::tvec2<int>, int> >, _Equal = KeyTraits, _H1 = KeyTraits, _H2 = std::__detail::_Mod_range_hashing, std::__detail::_Hash_code_base<_Key, _Value, _ExtractKey, _Equal, _H1, _H2, std::__detail::_Default_ranged_hash, false>::_Hash_code_type = long unsigned int]’: 
/usr/include/c++/4.5/bits/hashtable_policy.h:535:74: instantiated from ‘std::__detail::_Map_base<_Key, _Pair, std::_Select1st<_Pair>, true, _Hashtable>::mapped_type& std::__detail::_Map_base<_Key, _Pair, std::_Select1st<_Pair>, true, _Hashtable>::operator[](const _Key&) [with _Key = glm::detail::tvec2<int>, _Pair = std::pair<const glm::detail::tvec2<int>, int>, _Hashtable = std::_Hashtable<glm::detail::tvec2<int>, std::pair<const glm::detail::tvec2<int>, int>, std::allocator<std::pair<const glm::detail::tvec2<int>, int> >, std::_Select1st<std::pair<const glm::detail::tvec2<int>, int> >, KeyTraits, KeyTraits, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, false, false, true>, std::__detail::_Map_base<_Key, _Pair, std::_Select1st<_Pair>, true, _Hashtable>::mapped_type = int]’ 
/home/arne/codelite/LanguageTest/main.cpp:24:16: instantiated from here 
/usr/include/c++/4.5/bits/hashtable_policy.h:727:25: error: passing ‘const KeyTraits’ as ‘this’ argument of ‘size_t KeyTraits::operator()(const glm::core::type::ivec2&)’ discards qualifiers 
/usr/include/c++/4.5/bits/hashtable_policy.h: In member function ‘bool std::__detail::_Hash_code_base<_Key, _Value, _ExtractKey, _Equal, _H1, _H2, std::__detail::_Default_ranged_hash, false>::_M_compare(const _Key&, std::__detail::_Hash_code_base<_Key, _Value, _ExtractKey, _Equal, _H1, _H2, std::__detail::_Default_ranged_hash, false>::_Hash_code_type, std::__detail::_Hash_node<_Value, false>*) const [with _Key = glm::detail::tvec2<int>, _Value = std::pair<const glm::detail::tvec2<int>, int>, _ExtractKey = std::_Select1st<std::pair<const glm::detail::tvec2<int>, int> >, _Equal = KeyTraits, _H1 = KeyTraits, _H2 = std::__detail::_Mod_range_hashing, std::__detail::_Hash_code_base<_Key, _Value, _ExtractKey, _Equal, _H1, _H2, std::__detail::_Default_ranged_hash, false>::_Hash_code_type = long unsigned int]’: 
/usr/include/c++/4.5/bits/hashtable.h:879:2: instantiated from ‘std::_Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, __cache_hash_code, __constant_iterators, __unique_keys>::_Node* std::_Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, __cache_hash_code, __constant_iterators, __unique_keys>::_M_find_node(std::_Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, __cache_hash_code, __constant_iterators, __unique_keys>::_Node*, const key_type&, typename std::_Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, __cache_hash_code, __constant_iterators, __unique_keys>::_Hash_code_type) const [with _Key = glm::detail::tvec2<int>, _Value = std::pair<const glm::detail::tvec2<int>, int>, _Allocator = std::allocator<std::pair<const glm::detail::tvec2<int>, int> >, _ExtractKey = std::_Select1st<std::pair<const glm::detail::tvec2<int>, int> >, _Equal = KeyTraits, _H1 = KeyTraits, _H2 = std::__detail::_Mod_range_hashing, _Hash = std::__detail::_Default_ranged_hash, _RehashPolicy = std::__detail::_Prime_rehash_policy, bool __cache_hash_code = false, bool __constant_iterators = false, bool __unique_keys = true, std::_Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, __cache_hash_code, __constant_iterators, __unique_keys>::_Node = std::__detail::_Hash_node<std::pair<const glm::detail::tvec2<int>, int>, false>, key_type = glm::detail::tvec2<int>, typename std::_Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, __cache_hash_code, __constant_iterators, __unique_keys>::_Hash_code_type = long unsigned int]’ 
/usr/include/c++/4.5/bits/hashtable_policy.h:540:53: instantiated from ‘std::__detail::_Map_base<_Key, _Pair, std::_Select1st<_Pair>, true, _Hashtable>::mapped_type& std::__detail::_Map_base<_Key, _Pair, std::_Select1st<_Pair>, true, _Hashtable>::operator[](const _Key&) [with _Key = glm::detail::tvec2<int>, _Pair = std::pair<const glm::detail::tvec2<int>, int>, _Hashtable = std::_Hashtable<glm::detail::tvec2<int>, std::pair<const glm::detail::tvec2<int>, int>, std::allocator<std::pair<const glm::detail::tvec2<int>, int> >, std::_Select1st<std::pair<const glm::detail::tvec2<int>, int> >, KeyTraits, KeyTraits, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, false, false, true>, std::__detail::_Map_base<_Key, _Pair, std::_Select1st<_Pair>, true, _Hashtable>::mapped_type = int]’ 
/home/arne/codelite/LanguageTest/main.cpp:24:16: instantiated from here 
/usr/include/c++/4.5/bits/hashtable_policy.h:742:48: error: passing ‘const KeyTraits’ as ‘this’ argument of ‘bool KeyTraits::operator()(const glm::core::type::ivec2&, const glm::core::type::ivec2&)’ discards qualifiers 
/usr/include/c++/4.5/bits/hashtable_policy.h: In member function ‘size_t std::__detail::_Hash_code_base<_Key, _Value, _ExtractKey, _Equal, _H1, _H2, std::__detail::_Default_ranged_hash, false>::_M_bucket_index(const std::__detail::_Hash_node<_Value, false>*, size_t) const [with _Key = glm::detail::tvec2<int>, _Value = std::pair<const glm::detail::tvec2<int>, int>, _ExtractKey = std::_Select1st<std::pair<const glm::detail::tvec2<int>, int> >, _Equal = KeyTraits, _H1 = KeyTraits, _H2 = std::__detail::_Mod_range_hashing, size_t = long unsigned int]’: 
/usr/include/c++/4.5/bits/hashtable.h:1170:59: instantiated from ‘void std::_Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, __cache_hash_code, __constant_iterators, __unique_keys>::_M_rehash(std::_Hashtable::size_type) [with _Key = glm::detail::tvec2<int>, _Value = std::pair<const glm::detail::tvec2<int>, int>, _Allocator = std::allocator<std::pair<const glm::detail::tvec2<int>, int> >, _ExtractKey = std::_Select1st<std::pair<const glm::detail::tvec2<int>, int> >, _Equal = KeyTraits, _H1 = KeyTraits, _H2 = std::__detail::_Mod_range_hashing, _Hash = std::__detail::_Default_ranged_hash, _RehashPolicy = std::__detail::_Prime_rehash_policy, bool __cache_hash_code = false, bool __constant_iterators = false, bool __unique_keys = true, std::_Hashtable::size_type = long unsigned int]’ 
/usr/include/c++/4.5/bits/hashtable.h:911:8: instantiated from ‘std::_Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, __cache_hash_code, __constant_iterators, __unique_keys>::iterator std::_Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, __cache_hash_code, __constant_iterators, __unique_keys>::_M_insert_bucket(const value_type&, std::_Hashtable::size_type, typename std::_Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, __cache_hash_code, __constant_iterators, __unique_keys>::_Hash_code_type) [with _Key = glm::detail::tvec2<int>, _Value = std::pair<const glm::detail::tvec2<int>, int>, _Allocator = std::allocator<std::pair<const glm::detail::tvec2<int>, int> >, _ExtractKey = std::_Select1st<std::pair<const glm::detail::tvec2<int>, int> >, _Equal = KeyTraits, _H1 = KeyTraits, _H2 = std::__detail::_Mod_range_hashing, _Hash = std::__detail::_Default_ranged_hash, _RehashPolicy = std::__detail::_Prime_rehash_policy, bool __cache_hash_code = false, bool __constant_iterators = false, bool __unique_keys = true, std::_Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, __cache_hash_code, __constant_iterators, __unique_keys>::iterator = std::__detail::_Hashtable_iterator<std::pair<const glm::detail::tvec2<int>, int>, false, false>, value_type = std::pair<const glm::detail::tvec2<int>, int>, std::_Hashtable::size_type = long unsigned int, typename std::_Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, __cache_hash_code, __constant_iterators, __unique_keys>::_Hash_code_type = long unsigned int]’ 
/usr/include/c++/4.5/bits/hashtable_policy.h:543:24: instantiated from ‘std::__detail::_Map_base<_Key, _Pair, std::_Select1st<_Pair>, true, _Hashtable>::mapped_type& std::__detail::_Map_base<_Key, _Pair, std::_Select1st<_Pair>, true, _Hashtable>::operator[](const _Key&) [with _Key = glm::detail::tvec2<int>, _Pair = std::pair<const glm::detail::tvec2<int>, int>, _Hashtable = std::_Hashtable<glm::detail::tvec2<int>, std::pair<const glm::detail::tvec2<int>, int>, std::allocator<std::pair<const glm::detail::tvec2<int>, int> >, std::_Select1st<std::pair<const glm::detail::tvec2<int>, int> >, KeyTraits, KeyTraits, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, false, false, true>, std::__detail::_Map_base<_Key, _Pair, std::_Select1st<_Pair>, true, _Hashtable>::mapped_type = int]’ 
/home/arne/codelite/LanguageTest/main.cpp:24:16: instantiated from here 
/usr/include/c++/4.5/bits/hashtable_policy.h:737:55: error: passing ‘const KeyTraits’ as ‘this’ argument of ‘size_t KeyTraits::operator()(const glm::core::type::ivec2&)’ discards qualifiers 

Respuesta

5

Necesita especificar una clase hash y una clase de comparación en su typedef. Hash y KeyEqual Ver plantilla params aquí: http://en.cppreference.com/w/cpp/container/unordered_map

que se verá así que (tener en cuenta la fase de clasificación const al final de las firmas de método):

struct KeyFuncs 
{ 
    size_t operator()(const ivec2& k)const 
    { 
     return std::hash<int>()(k.x)^std::hash<int>()(k.y); 
    } 

    bool operator()(const ivec2& a, const ivec2& b)const 
    { 
      return a.x == b.x && a.y == b.y; 
    } 
}; 


typedef unordered_map<ivec2,int,KeyFuncs,KeyFuncs> MyMap; 
+0

Lo siento, cometí un error al exportar mi problema al desbordamiento de la pila. El programa debería ser ahora mismo. Mi problema es que esos mensajes de error de la plantilla son totalmente inútiles para mí, porque no puedo leerlos. – Arne

+0

¿Lo resolvió? Echa un vistazo a los calificadores const (ver edición). – Sam

+0

De nada. En esta ocasión, hice una edición para dar un nombre más apropiado. – Sam

2

Basta con mirar el error de vinculador, que le dice lo que debe aplicar o proporcionar en la lista de argumentos de plantilla:

std::hash<glm::detail::tvec2<int> >::operator()(glm::detail::tvec2<int>) const 

El programa no sabe cómo crear un hash basado en el vector objeto. Tendría que calcular su propio hash, para que el código del mapa pueda diferenciar entre vectores.

Editar sección: Tendería a utilizar punteros a vectores, ya que esto podría arruinarse si agrega algún elemento y lo cambia más adelante (por lo que debe agregar objetos const).


Edición 2: Con el mensaje de código/error de actualización, parece que se olvidó de crear los métodos dentro KeyTaits const, por lo que su puntero this es de tipo KeyTraits*, pero el valor pasado está destinado a ser const KeyTraits* .

+0

he cambiado mi problema un poco, debido a que la El primer mensaje de error aquí no era mi problema real, era más bien un error al portarlo correctamente en un Proyecto externo. – Arne

+0

Actualizado mi respuesta. – Mario

+0

en este caso Los punteros no están haciendo el trabajo, porque calculo un vector en otro lugar y necesito verificar si existe una clave para este objeto. Hacerlo con punteros siempre no daría ningún valor, porque sus direcciones son diferentes. – Arne

Cuestiones relacionadas