C++ C++;函数作为参数错误
我试图在我创建的二进制搜索树类中遍历的每个节点上运行一个函数。以下是遍历BST节点并在每个节点上运行作为参数传入的函数的函数:C++ C++;函数作为参数错误,c++,templates,compiler-errors,function-parameter,C++,Templates,Compiler Errors,Function Parameter,我试图在我创建的二进制搜索树类中遍历的每个节点上运行一个函数。以下是遍历BST节点并在每个节点上运行作为参数传入的函数的函数: template<class ItemType, class OtherType> void BinarySearchTree<ItemType, OtherType>::Inorder(void visit(BinaryNode<ItemType, OtherType>&), BinaryNode<ItemType,
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
当我编译时,我得到了这个错误,我不知道如何修复它
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;
MainBST.cpp:62:29:错误:无法初始化“void”类型的参数
(*)(BinaryNode&)'带
“void(string&)”类型的左值(也称为“void(basic_string&”):第一个参数的类型不匹配
('BinaryNode&'vs
'字符串&'(又称为'基本字符串')
&'))
tree1Ptr->InorderTraverse(显示项);
^~~~~~~~~~~
./BinarySearchTree.h:42:29:注意:在此处将参数传递给参数“visit”
void InorderTraverse(void访问(BinaryNode&))常量;
如果有人能理解错误并能破译它并帮助我,我将非常感激。如果你需要我写更多的代码,我很乐意。非常感谢你
错误:无法初始化“void”类型的参数
(*)(BinaryNode,LinkedQueue>&)'
类型为“void(string&)”的左值(也称为“void(basic_string,allocator>&)”):第一个参数的类型不匹配
('BinaryNode,LinkedQueue>&'vs
'string&'(又称'basic_string,分配器>
&("))
把它拆了
无法初始化类型为的参数
使用错误类型的参数调用了函数
'void(*)(BinaryNode,LinkedQueue>&)'
期望类型
具有类型为“void(string&)”的左值
提供的类型
函数是用voiddisplayitem(std::string&)
调用的,而不是voiddisplayitem(BinaryNode&anItem)
解决方案:确保在首次使用之前声明了
void displayItem(BinaryNode&anItem)
。可能会搜索并删除或重命名void displayItem(std::string&)
以防止将来的混淆。找到另一个displayItem
了吗?编译器非常确定displayItem
采用的是string
,而不是BinaryNode
。也许是一份陈旧的远期声明?哦,天哪,太谢谢你了。哇,我已经盯着这个项目看了8个多小时了,我都忘了申报了。只是,我更新了定义,但没有更新声明。非常非常感谢。
void displayItem(BinaryNode<string, LinkedQueue<int> >& anItem)
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;