我想用template来实现一个优先级队列。我试过了但我收到了一些错误,我想通过自定义谓词支持来减少函数的使用。支持谓词的C++模板函数
#include <iostream>
using namespace std;
template <typename T, std::size_t N, typename lessFunction>
class MyClass
{
typedef std::size_t size_type;
public:
void push(const T& t) {
//
size_type index ;//(some value .. say 5)
//...//
if(lessFunction(m_buffer[index], t))
{
/// do something
}
}
private:
T m_buffer[N];
};
struct myCompare
{
bool operator() (int& x, const int& y) {
return abs(x) < abs(y);
}
};
int main()
{
MyClass<int , 8, myCompare> obj;
obj.push(1);
return 0;
}
我得到这个错误。
/home/sanju/code/circular-buffer/main.cpp:17: error: no matching function for call to 'myCompare::myCompare(int&, const int&)'
if(lessFunction(m_buffer[index], t))
请纠正我。我还有一个问题 这个模板如何使用一个函子以及函数lessFunction?
less功能是*类型*。要调用'lessFunction :: operator()',你需要一个*对象*。 –