Reputation: 10156
Using MSVC++ 2010, defining a templated class member outside its declaration block:
template <typename T> class cls {
public:
template <typename T> void bar(T x);
};
template <typename T> void cls<T>::bar(T x) {}
yields:
unable to match function definition to an existing declaration
1> definition
1> 'void cls<T>::bar(T)'
1> existing declarations
1> 'void cls<T>::bar(T)'
why?
Upvotes: 3
Views: 6905
Reputation: 55395
If you're intention is for bar
to be a member template, then you need this:
template <typename T> class cls {
public:
template <typename U> void bar(U x);
};
template<typename T>
template<typename U>
void cls<T>::bar(U x) { }
Note that, the template parameter of the member must not shadow the class template parameter, hence I changed it to U
.
Upvotes: 0
Reputation: 96810
You need two template declarations because each construct works on a different template argument:
template <typename P>
template <typename T>
void cls<P>::bar(T x) {}
But it seems to me that bar
does not need to be templated at all. Use this instead:
template <typename T>
class cls
{
public:
void bar(T x);
};
template <typename T> void cls<T>::bar(T x) {}
Upvotes: 6