我有一些像这样的代码:
template <class Item,class Key>
class bst
{
public:
//bst(const Item& new_item,const Key& new_key);
Key get_key() const {return key;};
Item get_item() const {return item;};
bst get_right() const {return *rightPtr;};
bst get_left() const {return *leftPtr;};
void set_key(const Key& new_key) {key = new_key;};
void set_item(const Item& new_item) {item = new_item;};
void set_right(bst *new_right) {rightPtr=new_right;};
void set_left(bst *new_left) {leftPtr=new_left;};
Item item;
Key key;
bst *rightPtr;
bst *leftPtr;
private:
};
template <class Item,class Key,class Process,class Param>
void inorder_processing_param(bst<Item,Key> *root,Process f,Param p)
{
if(root==NULL)
{return;}
else
{
inorder_processing(root->leftPtr,f,p);
f(root->item,p);
inorder_processing(root->rightPtr,f,p);
}
}
void perfect(studentRecord s)
{
if (s.GPA==4.0)
{
cout << s.id << " " << s.student_name;
}
}
void major_m(bst<studentRecord,int>* root)
{
if (root->item.major=="m")
{
cout << root->item.id << " " << root->item.student_name;
}
}
void print_major(bst<studentRecord,int>* root,char* m)
{
inorder_processing(root,major_m);
}
运行时出现此错误:
bst.template: In function `void inorder_processing(bst<Item, Key>*, Process)
[with Item = studentRecord, Key = int, Process = void (*)(bst<studentRecord,
int>*)]':
studentDatabase.cxx:97: instantiated from here
bst.template:151: error: cannot convert `studentRecord' to `bst<studentRecord,
int>*' in argument passing
我该如何解决?
答案 0 :(得分:1)
变化:
f(root->item,p);
要:
f(root);
或者,将另一个参数添加到major_m
并将其称为f(root, p)
编辑: 显然你还没有发布你的
template <class Item,class Key,class Process,class Param>
void inorder_processing(bst<Item,Key> *root,Process f)
功能。根据你拥有的代码,当你需要做f(root->item)
f(root)