我想用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()',你需要一個*對象*。 –