0x1337
0x1337

Reputation: 1104

Prevent template partial specialization error

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

Answers (1)

Kerrek SB
Kerrek SB

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

Related Questions