头文件中的仿函数

itc*_*lpl 0 c++ functor

我有以下仿函数,我把它包含在我的主程序中

template<class T> struct Comp: public binary_function<T, T, int>
{
 int operator()(const T& a, const T& b) const
 {
   return (a>b) ? 1: (a<b) ? -1 :0;
 }
};
Run Code Online (Sandbox Code Playgroud)

当它在.cpp中时没有给出任何错误,但现在当我将它移动到我的.h时,它给了我以下错误:

testclass.h: At global scope:
testclass.h:50:59: error: expected template-name before ‘<’ token
testclass.h:50:59: error: expected ‘{’ before ‘<’ token
testclass.h:50:59: error: expected unqualified-id before ‘<’ token
Run Code Online (Sandbox Code Playgroud)

所以,我把它重写为:

template<class T> T Comp: public binary_function<T, T, int>
{
 int operator()(const T& a, const T& b) const
 {
   return (a>b) ? 1: (a<b) ? -1 :0;
 }
};
Run Code Online (Sandbox Code Playgroud)

现在我收到以下错误:

testclass.h: At global scope:
testclass.h:50:30: error: expected initializer before ‘:’ token
Run Code Online (Sandbox Code Playgroud)

关于如何解决它的任何建议?谢谢!

Unc*_*ens 6

原始错误可能是binary_function:缺少include或不考虑它在命名空间中std.

#include <functional>
Run Code Online (Sandbox Code Playgroud)

std::binary_function<T, T, int>
Run Code Online (Sandbox Code Playgroud)