【发布时间】:2021-12-30 19:05:50
【问题描述】:
我是 C++ 新手,所以这可能是一个简单的错误,但是这段代码现在给我带来了几个小时的问题。我真的只是不确定下一步该尝试什么。
EratosthenesHashMap.h
#pragma once
#include <unordered_map>
#include <boost/functional/hash.hpp>
#include "SieveOfEratosthenes.h"
template<class T>
class EratosthenesHashMap
{
public:
EratosthenesHashMap(SieveOfEratosthenes& sieve);
~EratosthenesHashMap();
unsigned int addValue(T& value);
unsigned int getPrime(T& value) const;
private:
SieveOfEratosthenes *sieve;
std::unordered_map<T, unsigned int, boost::hash<T>> valueMap;
};
EratosthenesHashMap.cpp
#include "EratosthenesHashMap.h"
EratosthenesHashMap<class T>::EratosthenesHashMap(SieveOfEratosthenes& sieve)
{
this->sieve = &sieve;
};
unsigned int EratosthenesHashMap<T>::addValue(T& value)
{
return 0;
}
unsigned int EratosthenesHashMap<T>::getPrime(T& value) const
{
return 0;
}
错误:
EratosthenesHashMap.cpp
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30037\include\utility(331,10): error C2079: 'std::pair<const T,unsigned int>::first' uses undefined class 'T'
1> with
1> [
1> T=T
1> ]
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30037\include\xhash(305): message : see reference to class template instantiation 'std::pair<const T,unsigned int>' being compiled
1> with
1> [
1> T=T
1> ]
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30037\include\xhash(304): message : while compiling class template member function 'void std::_Hash_vec<std::allocator<std::_List_unchecked_iterator<std::_List_val<std::_List_simple_types<_Ty>>>>>::_Tidy(void) noexcept'
1> with
1> [
1> _Ty=std::pair<const T,unsigned int>
1> ]
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30037\include\xhash(313): message : see reference to function template instantiation 'void std::_Hash_vec<std::allocator<std::_List_unchecked_iterator<std::_List_val<std::_List_simple_types<_Ty>>>>>::_Tidy(void) noexcept' being compiled
1> with
1> [
1> _Ty=std::pair<const T,unsigned int>
1> ]
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30037\include\xhash(1933): message : see reference to class template instantiation 'std::_Hash_vec<std::allocator<std::_List_unchecked_iterator<std::_List_val<std::_List_simple_types<_Ty>>>>>' being compiled
1> with
1> [
1> _Ty=std::pair<const T,unsigned int>
1> ]
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30037\include\unordered_map(69): message : see reference to class template instantiation 'std::_Hash<std::_Umap_traits<_Kty,_Ty,std::_Uhash_compare<_Kty,_Hasher,_Keyeq>,_Alloc,false>>' being compiled
1> with
1> [
1> _Kty=T,
1> _Ty=unsigned int,
1> _Hasher=boost::hash<T>,
1> _Keyeq=std::equal_to<T>,
1> _Alloc=std::allocator<std::pair<const T,unsigned int>>
1> ]
1>C:\Users\jpsie\source\repos\EratosthenesContainer\EratosthenesContainer\EratosthenesHashMap.h(20): message : see reference to class template instantiation 'std::unordered_map<T,unsigned int,boost::hash<T>,std::equal_to<T>,std::allocator<std::pair<const T,unsigned int>>>' being compiled
1> with
1> [
1> T=T
1> ]
我正在尝试创建一个 hashmap 作为键类型为 T、值类型为 unsigned int 的成员变量,并且我正在将 boost 库用于哈希函数。
【问题讨论】:
-
1.您忘记了每个方法定义之前的
template<class T>。 2 Why can templates only be implemented in the header file?. -
来自 MSVC 的错误消息在这里真的没有帮助。似乎假设您正在尝试显式专业化,但其语法也会有所不同。它应该只是在尝试定义
EratosthenesHashMap::EratosthenesHashMap时失败并出现语法错误。 -
@user17732522, 由于
class T是声明类内联的合法方式,编译器可能采用EratosthenesHashMap<class T>,精确地实例化模板因为这种语法不能是专业化或任何东西,因此使用std::pair的std::unordered_map,然后最终看到包含不完整类作为成员的一对将不起作用。错误本身对我来说很有意义,它只有几层深。甚至可能是编译器看到EratosthenesHashMap<class T>并回退到“这必须以 ... 类型开始声明”。 -
@chris 这可能是一种解释,但 MSVC 只接受没有
template<>介绍的显式特化。如果您将class T更改为T您会得到相同的错误,并且使用int而不是T它可以愉快地编译。 -
@user17732522,很有趣。在那种情况下,我撤销我所说的话。这似乎是一个令人高兴的巧合,它符合这种逻辑。
标签: c++