C++ 使用std::hash时发生编译错误;不命名模板类型
我收到了错误 hashmap.hpp:63:14:错误:命名空间“std”中的“hash”未命名 模板类型std::哈希 我不明白为什么在传递类型t时需要实现自己的专门化,t可能是哈希函数的工作类型之一 hashmap.hppC++ 使用std::hash时发生编译错误;不命名模板类型,c++,templates,c++11,compiler-errors,stdhash,C++,Templates,C++11,Compiler Errors,Stdhash,我收到了错误 hashmap.hpp:63:14:错误:命名空间“std”中的“hash”未命名 模板类型std::哈希 我不明白为什么在传递类型t时需要实现自己的专门化,t可能是哈希函数的工作类型之一 hashmap.hpp #ifndef __HASHMAP_HPP__ #define __HASHMAP_HPP__ #define INITIAL_CAPACITY 10 #include <iostream> #include <functional> // A
#ifndef __HASHMAP_HPP__
#define __HASHMAP_HPP__
#define INITIAL_CAPACITY 10
#include <iostream>
#include <functional>
// A simple node struct for your separate chaining
// You may change this if you want
template <class K,class V>
struct KVNode
{
K key;
V value;
KVNode<K,V>* next;
KVNode(KVNode<K,V>* theNext = NULL) :
next(theNext)
{
}
};
template <class K, class V>
class HashMap {
public:
// Default constructor: creates an empty hash map
HashMap();
// Constructor setting the default value for our map
HashMap(V value);
// Destructor: deallocates all memory associated
// with the hash map
~HashMap();
// size() returns the number of elements currently
// stored in the hash map.
unsigned int size() const;
// set() inserts the given key value pair into the
// hash map. If the key is already in the map, then
// the value for that key is overwritten.
void set(const K& key, const V& value);
// get() returns the value associated with the key.
// If the key is not in the hash map, the default
// value should be returned.
V get(const K& key) const;
private:
// You are allowed to add any private variables you
// need to implement the functionality of a hash map
// It is also okay to add addional public or private
// methods you need, but you may not change the
// already existing method signatures.
// You may find these private members useful, but you are
// not required to use them.
KVNode<K,V>** map;
// To use this hash map with your own key type,
// you may need to provide a specialization of std::hash.
//std::hash<K> hash; #this was previous declaration but caused error
std::hash<K> hash;
// This is the default value to return if a key is not
// in the hash map
V default_value;
};
template <class K, class V>
HashMap<K,V>::HashMap() : default_value()
{
KVNode<K,V>** map = new KVNode<K,V>*[INITIAL_CAPACITY];
for (int i = 0; i < INITIAL_CAPACITY; ++i)
map[i] = new KVNode<K,V>();
}
template <class K, class V>
HashMap<K,V>::HashMap(V value)
{
//TODO: Implement this method
}
template <class K, class V>
HashMap<K,V>::~HashMap()
{
//TODO: Implement this method
}
template <class K, class V>
unsigned int HashMap<K,V>::size() const
{
//TODO: Implement this method
}
template <class K, class V>
void HashMap<K,V>::set(const K& key, const V& value)
{
//TODO: Implement this method
}
template <class K, class V>
V HashMap<K,V>::get(const K& key) const
{
//TODO: Implement this method
}
#endif // __HASHMAP_HPP__
\ifndef\uuuHashMap\uHPP__
#定义\uuuuHashMap\uHPP__
#定义初始容量10
#包括
#包括
//用于单独链接的简单节点结构
//如果你愿意,你可以改变这个
模板
结构KVNode
{
K键;
V值;
KVNode*下一步;
KVNode(KVNode*theNext=NULL):
下一个(下一个)
{
}
};
模板
类哈希映射{
公众:
//默认构造函数:创建空哈希映射
HashMap();
//构造函数设置映射的默认值
HashMap(V值);
//析构函数:取消分配所有关联的内存
//使用哈希映射
~HashMap();
//size()返回当前的元素数
//存储在哈希映射中。
无符号整数大小()常量;
//set()将给定的键值对插入到
//哈希映射。如果密钥已在映射中,则
//该键的值将被覆盖。
无效集(常数K和键、常数V和值);
//get()返回与键关联的值。
//如果密钥不在哈希映射中,则默认值为
//应返回值。
V get(const K&key)const;
私人:
//您可以添加任何您需要的私有变量
//需要实现哈希映射的功能
//也可以添加额外的公共或私人
//方法,但不能更改
//已存在的方法签名。
//你可能会发现这些私人成员很有用,但你确实是
//不需要使用它们。
KVNode**map;
//要将此哈希映射与您自己的密钥类型一起使用,
//您可能需要提供std::hash的专门化。
//std::hash hash;#这是以前的声明,但导致了错误
std::散列;
//这是在未指定键时返回的默认值
//在散列映射中
V默认_值;
};
模板
HashMap::HashMap():默认值()
{
KVNode**map=新KVNode*[初始容量];
对于(int i=0;i<初始容量;++i)
map[i]=新的KVNode();
}
模板
HashMap::HashMap(V值)
{
//TODO:实现此方法
}
模板
HashMap::~HashMap()
{
//TODO:实现此方法
}
模板
无符号int HashMap::size()常量
{
//TODO:实现此方法
}
模板
void HashMap::set(常量K和key、常量V和value)
{
//TODO:实现此方法
}
模板
V HashMap::get(const K&key)const
{
//TODO:实现此方法
}
#endif/\uuuuHashMap\uHPP__
临时cpp
#include "hashmap.hpp"
#include <iostream>
#include <string>
#include <functional>
using namespace std;
int main()
{
HashMap<string,int> hm = HashMap<string,int>();
return 0;
}
#包括“hashmap.hpp”
#包括
#包括
#包括
使用名称空间std;
int main()
{
HashMap hm=HashMap();
返回0;
}
是从C++11引入的,您需要C++11支持才能使用它
如果您使用GCC,则需要添加编译选项-std=c++11
是从c++11引入的,使用它需要c++11支持
如果使用GCC,则需要添加编译选项
-std=c++11
编译器是什么?试着用选项-std=c++11
编译它。这就成功了!非常感谢。如果你把它作为常规解决方案发布,我可以给你答案。编译器是什么?试着用选项-std=c++11
编译它。这就成功了!非常感谢。如果你把它作为一个常规的解决方案,我可以给你答案。