-1
我定義了一個類,然後在priority_queue中保存指向Foo的指針,並使用我定義的cmp函數。函數對象和函數指針之間的區別?
但如果CMP-funtion調用函數對象時,發生錯誤:
class Foo
{
friend bool cmp(Foo *, Foo *);
public:
Foo() = default;
Foo(int x):val(x) {}
private:
int val;
};
bool cmp(Foo *a, Foo *b)
{
return a->val < b->val;
}
int main()
{
priority_queue<Foo*, vector<Foo*>, decltype(cmp)*> que;
que.push(new Foo(5));
que.push(new Foo(6));
return 0;
}
的functione對象正常運行。
class Foo
{
friend struct cmp;
public:
Foo() = default;
Foo(int x):val(x) {}
private:
int val;
};
struct cmp
{
bool operator()(Foo *a, Foo *b)
{
return a->val < b->val;
}
};
int main()
{
priority_queue<Foo*, vector<Foo*>, cmp> que;
que.push(new Foo(5));
que.push(new Foo(6));
return 0;
}
IDE is code :: blocks – Wonter
「發生錯誤」?真? –
未編譯錯誤 – Wonter