错误:不能在参数传递中转换

error: cannot convert in arguement passing

本文关键字:转换 参数传递 不能 错误      更新时间:2023-10-16

我有一些这样的代码:

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)