Lucas Lu
Lucas Lu

Reputation: 31

constructor for a class with template using iterators

I have a Hashmap Class in the header

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

How do I declare this in the cpp file?

I tried:

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

It does not work. Thanks.

Upvotes: 2

Views: 51

Answers (1)

cigien
cigien

Reputation: 60208

You need separate templates for the class and the constructor, like this:

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

Note that the out of line definition of the constructor can't specify the default template parameters.

Also, your question says you want to put this in the .cpp file, but you shouldn't do that. Templates should always be in header files.

Upvotes: 4

Related Questions