我有一个类似于以下的课程。我正在使用需要比较器作为模板参数的 boost 库的配对堆。我的比较器应该访问 A 类的数据和成员以进行比较。最初,我将 'my_compare' 声明为结构并重载 () 运算符。但是除非将指向 A 类的指针('this')传递给它,否则该结构无法访问 A 类的数据。但这意味着 my_compare 不再是编译时常量,并且会产生错误:“this”不能出现在常量表达式中。
作为第二次尝试,我将 my_compare 声明为成员函数(以便它可以访问成员和数据)。我现在收到以下错误:
error: type/value mismatch at argument 1 in template parameter list for
‘template<class T> struct boost::heap::compare’
我怀疑有两种可能的解释:“my_compare”不是(函数)对象,也不是二进制函数,因为“this”是隐式传递的。我该如何解决这个问题。
class A{
public:
//some data(properties)
struct c{
//some data
};
double method1(int variable);
double method2(const struct c&);
bool my_compare(struct c& c, struct c& d){
//accesses member methods and data
}
typedef boost::heap::pairing_heap<struct c, boost::heap::compare<my_compare> > myheap;
}