gpt4 book ai didi

c++ - 错误 : cannot convert in arguement passing

转载 作者:行者123 更新时间:2023-11-28 08:21:06 24 4
gpt4 key购买 nike

我有这样的代码:

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

我该如何解决

最佳答案

改变:

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)/p>

关于c++ - 错误 : cannot convert in arguement passing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5828910/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com