【发布时间】:2013-12-17 06:36:58
【问题描述】:
我在做 C++ Primer 5th Edition 的练习时卡住了,就像
练习 11.11:不使用 decltype 重新定义书店。
以下是本书的相关代码:
multiset<Sales_data, decltype(compareIsbn)*> bookstore(compareIsbn);
Sales_data 类的代码写在这里有点冗长,所以我写了一个更简单的代码,并以相同的样式定义了 multiset,如下所示。它编译没有任何错误。
class A
{
int lenth;
public:
int getLenth() const {return lenth;}
};
bool compareA(const A &a1, const A &a2)
{
return a1.getLenth() < a2.getLenth();
}
int main()
{
std::multiset<A, decltype(compareA)*> m1(compareA);
return 0;
}
我猜这个练习是想让读者复习函数指针的知识,所以我尝试了另一种定义它的方法。但它不起作用。下面是我卡住的地方
int main()
{
bool (*fp) (const A &a1, const A &a2);
fp = &compareA;
std::multiset<A, fp*> m1(fp);
return 0;
}
产生了三个错误:
error: template argument 2 is invalid
error: invalid type in declaration before '(' token
error: invalid conversion from 'bool (*)(const A&, const A&)' to 'int' [-fpermissive]
我的问题是什么?如何解决?
【问题讨论】:
标签: c++ stl function-pointers