继续说这是功课。我正在尝试编译一个使用虚函数和模板的程序。但是我收到了错误:
错误:'<'之前的预期模板名称代币 struct compareItem:binary_function {
错误:在'<'之前预期'{'代币 错误:'<'之前的预期unqualified-id令牌
这是main.C
#include "Item.h"
int main(){
priority_queue<Item*, vector<Item*>, compareItem> pq;
W* w = new W(0, &pq);
T* t = new T(10, &pq);
pq.push(w);
pq.push(t);
while(!pq.empty()){
Item* i = pq.top();
pq.pop();
i->Run();
}
return 0;
}
这是Item.h
#ifndef ITEM_H
#define ITEM_H
#include <functional>
#include <queue>
#include <vector>
#include <iostream>
class Item{
int key;
public:
Item(int k) : key (k){}
friend class compareItem;
int getKey() {return key;}
void setkey(int x) {key = x;}
virtual void Run(){}
virtual ~Item(){}
};
struct compareItem : binary_function<Item*, Item*, bool>{
bool operator()(const Item* t1, const Item* t2) const{
return (t1->key > t2->key);
}
};
class W : public Item{
priority_queue<Item*, vector<Item*>, compareItem>* q;
public:
W(int k, priority_queue<Item*, vector<Item*>, compareItem >* q1) : Item (k), q(q1){}
void Run() {cout << "W at " << getKey() << endl;
setKey(getKey()+10);
q->push(this);
~W(){}
};
class T : public Item{
priority_queue<Item*, vector<Item*>, compareItem >* 1;
public:
T(int k, priority_queue<Item*, vector<Item*>, compareItem >* q1) : Item (k), q(q1){}
void Run() {cout << "T at " << getKey() << endl;
setKey(getKey()+25);
q->push(this);
~T(){}
};
#endif