2016-12-05 4 views
2

私が作成したバイナリ検索ツリークラスで横断された各ノードで関数を実行しようとしています。ここではBSTのノードを横断し、各ノードの引数として渡された関数を実行する機能は次のとおりです。パラメータエラーとしてC++関数

template<class ItemType, class OtherType> 
void BinarySearchTree<ItemType, OtherType>::Inorder(void visit(BinaryNode<ItemType, OtherType>&), BinaryNode<ItemType, OtherType>* node_ptr) const { 
    if (node_ptr != nullptr) { 
     Inorder(visit, node_ptr->GetLeftPtr()); 
     BinaryNode<ItemType, OtherType> node = *node_ptr; 
     visit(node); 
     Inorder(visit, node_ptr->GetRightPtr()); 
    } // end if 
} // end inorder 

これはBSTクラスのプライベートメンバ関数であるので、それは公共のメンバーによって呼び出されます機能:

void displayItem(BinaryNode<string, LinkedQueue<int> >& anItem) 

トラバーサルはこのように呼ばれている:私のメインのファイルで

template<class ItemType, class OtherType> 
void BinarySearchTree<ItemType, OtherType>::InorderTraverse(void visit(BinaryNode<ItemType, OtherType>&)) const 
{ 
    Inorder(visit, root_); 
} // end inorderTraverse 

、私は、引数として渡すために、この関数を作成し

tree1Ptr->InorderTraverse(displayItem); 

私がコンパイルすると、このエラーが発生し、どのように修正するかわかりません。

MainBST.cpp:62:29: error: cannot initialize a parameter of type 'void 
     (*)(BinaryNode<std::__1::basic_string<char>, LinkedQueue<int> > &)' with 
     an lvalue of type 'void (string &)' (aka 'void (basic_string<char, 
     char_traits<char>, allocator<char> > &)'): type mismatch at 1st parameter 
     ('BinaryNode<std::__1::basic_string<char>, LinkedQueue<int> > &' vs 
     'string &' (aka 'basic_string<char, char_traits<char>, allocator<char> > 
     &')) 
    tree1Ptr->InorderTraverse(displayItem); 
          ^~~~~~~~~~~ 
./BinarySearchTree.h:42:29: note: passing argument to parameter 'visit' here 
    void InorderTraverse(void visit(BinaryNode<ItemType, OtherType>&)) const; 

誰もがエラーを理解し、それを解読して助けてくれたら、私はとても感謝しています。より多くのコードを削除する必要がある場合、私はそれを行うことをうれしく思います。どうもありがとうございます!

+0

そこに別の 'displayItem'がありますか?コンパイラの 'displayItem'は、' BinaryNode'ではなく 'string'を取ります。おそらく、古くからの宣言ですか? – user4581301

+0

ああああ、ありがとう。うわー、私は宣言について忘れていた8時間以上このプロジェクトを見てきました。それだけで、私は定義を更新しましたが、宣言は更新しませんでした。本当にありがとう。 – Maximka

答えて

0

error: cannot initialize a parameter of type 'void (*)(BinaryNode, LinkedQueue > &)' with an lvalue of type 'void (string &)' (aka 'void (basic_string, allocator > &)'): type mismatch at 1st parameter ('BinaryNode, LinkedQueue > &' vs 'string &' (aka 'basic_string, allocator > &'))

ブレイクダウン!

cannot initialize a parameter of type

パラメータが間違った型で関数が呼び出されました。タイプ

英語の翻訳を提供

'void (*)(BinaryNode, LinkedQueue > &)'

期待タイプ

with an lvalue of type 'void (string &)'

:機能はvoid displayItem(std::string &)、ないvoid displayItem(BinaryNode<string, LinkedQueue<int> >& anItem)と呼ばれていました。

解決策:void displayItem(BinaryNode<string, LinkedQueue<int> >& anItem)が最初に使用される前に宣言されていることを確認してください。将来の混乱を避けるために、可能であればvoid displayItem(std::string &)を検索して削除または名前を変更してください。

関連する問題