为什么我不能在btAlignedObjectArray中使用函子作为比较器函数:quicksort?

时间:2013-08-13 05:40:51

标签: c++ arrays sorting bulletphysics

这是我的代码:

#include "btScalar.h"
#include "btAlignedAllocator.h"
#include "btAlignedObjectArray.h"

class Comparator
{
    btAlignedObjectArray<int> items;

    // Edit: this member function is to be called from within the comparator
    int myReference()
    {
        return 0;
    }

public:
    Comparator()
    {
        items.push_back(5);
        items.push_back(1);
        items.push_back(3);
        items.push_back(8);     
    }

    int operator()(const int &a, const int &b) const
    {
        return a + myReference() < b;
    }

    void doSort()
    {
        items.quickSort(*this);

        for (int i=0; i<items.size(); i++) {
            printf("%d\n", items[i]);
        }
    }
};

int main()
{
    Comparator myClass;

    myClass.doSort();

    printf("done!\n");

    return 0;
}

错误为No matching function for call to object of type 'const Comparator'

btAlignedObjectArray.h

中的第345和347行

1 个答案:

答案 0 :(得分:1)

尝试将const添加到operator()

int operator()(const int &a, const int &b) const
//                                         ^^^^^
{
    return a < b;
}

同样,你已经自己捕获了,myReference也应该是const

int myReference() const
//                ^^^^