c++ - 使用迭代器的模板类的构造函数

标签 c++

我在标题中有一个 Hashmap 类

template <typename K, typename M, typename H = std::hash<K>>
class HashMap {
public: 
template <class Iterator>
    HashMap(const Iterator& begin, const Iterator& end);
};

如何在 cpp 文件中声明它?

我尝试过:

template <class <typename K, typename M, typename H> Iterator>
HashMap<K, M, H>::HashMap(const Iterator& begin, const Iterator& end)

它不起作用。谢谢。

最佳答案

您需要类和构造函数的单独模板,如下所示:

template <typename K, typename M, typename H>
  template <class Iterator>
    HashMap<K,M,H>::HashMap(const Iterator& begin, const Iterator& end) {
 // ...
}

请注意,构造函数的越界定义不能指定默认模板参数。

此外,您的问题表明您想将其放入 .cpp 文件中,但您不应该这样做。模板应始终位于头文件中。

关于c++ - 使用迭代器的模板类的构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62399437/

相关文章:

C++ 处理特定的 impl - #ifdef vs 私有(private)继承 vs 标签调度

c++ - 从客户端获取输入到服务器

c++ - 如何调试.so源?

c++ - 使用 seekg() C++ 倒带流

c++ - 如何在不抛出的情况下构造 <stdexcept> 或 <system_error> 异常?

c++ - 单元测试私有(private)方法是一种好习惯吗?

c++ - 是否已经实现了任何 RAII 文件句柄?

c++ - 成员构造函数和析构函数调用的顺序

c++ - 重写 QDataStream 运算符 >> () 等同于没有 Qt 的 C++

c++ - 如何从 Windows 库的 GUID 获取 PIDL?