2011-04-29 13 views
0

轉換我有一些像這樣的代碼:錯誤:無法在arguement傳球

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

你也可以將行97粘貼到'studentDatabase.cxx'嗎? – evnu 2011-04-29 07:00:56

回答

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)