Reputation: 1104
There is a code:
#include <functional>
template<typename DataType, typename Compare=std::less<DataType>>
class MyClass {
public:
explicit MyClass(const Compare& f = Compare()) {
compare = f;
};
bool foo(DataType, DataType);
private:
Compare compare;
};
template<typename DataType>
bool MyClass<DataType>::foo(DataType a, DataType b) {
return compare(a, b);
}
which while compilation is getting an error:
error: nested name specifier 'MyClass<DataType>::'
for declaration does not refer into a class, class template or class
template partial specialization bool MyClass<DataType>::foo(DataType a, DataType b) {
How to prevent the error and declare the method outside the class?
Upvotes: 1
Views: 489
Reputation: 476940
You have to provide the template parameters as in the primary template definition:
// vvvvvvvvvvvvvvvvvvvvvvvvvvvvv
template <typename DataType, typename X>
bool MyClass<DataType, X>::foo(DataType a, DataType b) {
// ^^^^^^^^^^^
return compare(a, b);
}
Upvotes: 4