错误:不能在参数传递中转换
error: cannot convert in arguement passing
我有一些这样的代码:
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)
相关文章:
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 使用指向成员的指针将成员函数作为参数传递
- 如何将参数传递给正在使用模板的类
- 传递到函数(C )时转换参数
- 将其转换为共享指针,并将其作为参数传递给另一个C++类
- 将参数传递给函数,获取父类参数,而不将参数强制转换为父类
- 参数传递转换浮点错误
- 如何强制转换变量成员以将其作为函数的引用参数传递
- 移动语义:从"类型&&"到"类型"的转换无效。模板:将未知参数传递给重载函数
- 数组在C++中作为参数传递时隐式转换为容器类
- Const参数传递:转换无效
- 将指向函数的指针数组作为"int"参数传递是否可以将地址转换为整数?
- 需要将cstring转换为byte*,并需要在c++函数中将其作为i/p参数传递
- 作为参数传递的字符指针正在转换为字符串
- 试图将数组作为参数传递,得到双精度[4]到双精度转换错误
- 将std::string作为函数参数传递时的c++类型转换
- 将参数传递给函数,该函数将自动转换为指针
- 将指针作为参数传递给引用时的c++指针强制转换
- 在将 lambda 作为参数传递时,我是否应该避免转换为 std::function
- 错误:不能在参数传递中转换