我正在尝试创建一个unordered_map,其中键的类型为char,值是指向我自己的自定义类的指针。这可能吗?每当我尝试编译时,我都会遇到其他错误:
$ g ++ -std = c ++ 0x -o spellchecker spellchecker.cpp -lstdc ++
spellchecker.cpp:17:27:错误:模板参数2无效
spellchecker.cpp:17:27:错误:模板参数5无效
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
class Node{
public:
Node();
~Node();
bool getletter;
bool iseofword;
char letter;
unordered_map<char, *Node> children; // line 17
};
我试过了:
unordered_map<char, Node> children; // line 17 (not pointer)
但我明白了:
g++ -std=c++0x -o spellchecker spellchecker.cpp -lstdc++
In file included from /usr/include/c++/4.6/bits/stl_algobase.h:65:0,
from /usr/include/c++/4.6/bits/char_traits.h:41,
from /usr/include/c++/4.6/ios:41,
from /usr/include/c++/4.6/ostream:40,
from /usr/include/c++/4.6/iostream:40,
from spellchecker.cpp:1:
/usr/include/c++/4.6/bits/stl_pair.h: In instantiation of ‘std::pair<const char, Node>’:
/usr/include/c++/4.6/bits/stl_function.h:486:12: instantiated from ‘std::_Select1st<std::pair<const char, Node> >’
/usr/include/c++/4.6/bits/hashtable_policy.h:789:20: instantiated from ‘std::__detail::_Hash_code_base<char, std::pair<const char, Node>, std::_Select1st<std::pair<const char, Node> >, std::equal_to<char>, std::hash<char>, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, false>’
/usr/include/c++/4.6/bits/hashtable.h:105:11: instantiated from ‘std::_Hashtable<char, std::pair<const char, Node>, std::allocator<std::pair<const char, Node> >, std::_Select1st<std::pair<const char, Node> >, std::equal_to<char>, std::hash<char>, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, false, false, true>’
/usr/include/c++/4.6/bits/unordered_map.h:44:11: instantiated from ‘std::__unordered_map<char, Node, std::hash<char>, std::equal_to<char>, std::allocator<std::pair<const char, Node> >, false>’
/usr/include/c++/4.6/bits/unordered_map.h:256:11: instantiated from ‘std::unordered_map<char, Node>’
spellchecker.cpp:17:28: instantiated from here
/usr/include/c++/4.6/bits/stl_pair.h:93:11: error: ‘std::pair<_T1, _T2>::second’ has incomplete type
spellchecker.cpp:8:7: error: forward declaration of ‘class Node’
spellchecker.cpp: In member function ‘int Trie::addword(std::string)’:
spellchecker.cpp:45:39: error: cannot convert ‘std::__detail::_Map_base<char, std::pair<const char, Node>, std::_Select1st<std::pair<const char, Node> >, true, std::_Hashtable<char, std::pair<const char, Node>, std::allocator<std::pair<const char, Node> >, std::_Select1st<std::pair<const char, Node> >, std::equal_to<char>, std::hash<char>, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, false, false, true> >::mapped_type {aka Node}’ to ‘Node*’ in assignment
spellchecker.cpp:52:39: error: cannot convert ‘std::__detail::_Map_base<char, std::pair<const char, Node>, std::_Select1st<std::pair<const char, Node> >, true, std::_Hashtable<char, std::pair<const char, Node>, std::allocator<std::pair<const char, Node> >, std::_Select1st<std::pair<const char, Node> >, std::equal_to<char>, std::hash<char>, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, false, false, true> >::mapped_type {aka Node}’ to ‘Node*’ in assignment
spellchecker.cpp:53:3: error: expected ‘;’ before ‘}’ token
spellchecker.cpp: In function ‘int main()’:
spellchecker.cpp:60:26: error: conversion from ‘Trie*’ to non-scalar type ‘Trie’ requested
In file included from /usr/include/c++/4.6/bits/hashtable.h:35:0,
from /usr/include/c++/4.6/unordered_map:45,
from spellchecker.cpp:3:
/usr/include/c++/4.6/bits/hashtable_policy.h: In member function ‘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 = char, _Pair = std::pair<const char, Node>, _Hashtable = std::_Hashtable<char, std::pair<const char, Node>, std::allocator<std::pair<const char, Node> >, std::_Select1st<std::pair<const char, Node> >, std::equal_to<char>, std::hash<char>, 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 = Node]’:
spellchecker.cpp:45:39: instantiated from here
/usr/include/c++/4.6/bits/hashtable_policy.h:549:24: error: ‘struct std::pair<const char, Node>’ has no member named ‘second’
/usr/include/c++/4.6/bits/hashtable_policy.h:550:26: error: ‘struct std::pair<const char, Node>’ has no member named ‘second’
In file included from /usr/include/c++/4.6/bits/stl_algobase.h:65:0,
from /usr/include/c++/4.6/bits/char_traits.h:41,
from /usr/include/c++/4.6/ios:41,
from /usr/include/c++/4.6/ostream:40,
from /usr/include/c++/4.6/iostream:40,
from spellchecker.cpp:1:
/usr/include/c++/4.6/bits/stl_pair.h: In constructor ‘std::pair<_T1, _T2>::pair(std::pair<_U1, _U2>&&) [with _U1 = char, _U2 = Node, _T1 = const char, _T2 = Node]’:
/usr/include/c++/4.6/bits/hashtable_policy.h:94:12: instantiated from ‘std::__detail::_Hash_node<_Value, false>::_Hash_node(_Args&& ...) [with _Args = {std::pair<char, Node>}, _Value = std::pair<const char, Node>]’
/usr/include/c++/4.6/ext/new_allocator.h:114:4: instantiated from ‘void __gnu_cxx::new_allocator<_Tp>::construct(__gnu_cxx::new_allocator<_Tp>::pointer, _Args&& ...) [with _Args = {std::pair<char, Node>}, _Tp = std::__detail::_Hash_node<std::pair<const char, Node>, false>, __gnu_cxx::new_allocator<_Tp>::pointer = std::__detail::_Hash_node<std::pair<const char, Node>, false>*]’
/usr/include/c++/4.6/bits/hashtable.h:485:6: 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_allocate_node(_Args&& ...) [with _Args = {std::pair<char, Node>}, _Key = char, _Value = std::pair<const char, Node>, _Allocator = std::allocator<std::pair<const char, Node> >, _ExtractKey = std::_Select1st<std::pair<const char, Node> >, _Equal = std::equal_to<char>, _H1 = std::hash<char>, _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 char, Node>, false>]’
/usr/include/c++/4.6/bits/hashtable.h:928:62: 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(_Arg&&, std::_Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, __cache_hash_code, __constant_iterators, __unique_keys>::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 _Arg = std::pair<char, Node>, _Key = char, _Value = std::pair<const char, Node>, _Allocator = std::allocator<std::pair<const char, Node> >, _ExtractKey = std::_Select1st<std::pair<const char, Node> >, _Equal = std::equal_to<char>, _H1 = std::hash<char>, _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 char, Node>, false, false>, std::_Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, __cache_hash_code, __constant_iterators, __unique_keys>::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.6/bits/hashtable_policy.h:549: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 = char, _Pair = std::pair<const char, Node>, _Hashtable = std::_Hashtable<char, std::pair<const char, Node>, std::allocator<std::pair<const char, Node> >, std::_Select1st<std::pair<const char, Node> >, std::equal_to<char>, std::hash<char>, 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 = Node]’
spellchecker.cpp:45:39: instantiated from here
/usr/include/c++/4.6/bits/stl_pair.h:137:40: error: using invalid field ‘std::pair<_T1, _T2>::second
答案 0 :(得分:3)
您的指针表示法不正确。 *节点无效。 *来自类型,T *不是*。
unordered_map<char, Node*> children;