私が作成したバイナリ検索ツリークラスで横断された各ノードで関数を実行しようとしています。ここでは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;
誰もがエラーを理解し、それを解読して助けてくれたら、私はとても感謝しています。より多くのコードを削除する必要がある場合、私はそれを行うことをうれしく思います。どうもありがとうございます!
そこに別の 'displayItem'がありますか?コンパイラの 'displayItem'は、' BinaryNode'ではなく 'string'を取ります。おそらく、古くからの宣言ですか? – user4581301
ああああ、ありがとう。うわー、私は宣言について忘れていた8時間以上このプロジェクトを見てきました。それだけで、私は定義を更新しましたが、宣言は更新しませんでした。本当にありがとう。 – Maximka