为什么必须是非成员函数我把自定义的对象,放到优先队列中,priority_queue默认的使用less 比较器,为什么
为什么必须是非成员函数
我把自定义的对象,放到优先队列中,priority_queue
默认的使用less <> 比较器,为什么,我自定义的类的operator《必须是非成员的,如果是成员行数就会编译通不过
pq.cpp: In member function `bool _less <T> ::operator()(T, T) [with T = test] ':
pq.cpp:49: instantiated from here
pq.cpp:30: passing `const test ' as `this ' argument of `bool
test::operator <(test) ' discards qualifiers
[解决办法]
如果传的是函数指针的话,用成员函数就必须是static的,否则就只能用非成员函数了,因为类的non static成员函数是会默认加上一个this参数的,所以就会出现参数类型不匹配了。
不过楼主给的信息不全,不确定是否这个原因
