编译器声明调用没有匹配的函数,即使原型中有一个
The compiler states that there's no matching function for a call, even though there is one in the prototype
我正在尝试实现一个二叉搜索树类,但编译器抛出错误。bstNode.h 文件在这里:
template <class Item, class Key>
class bstNode
{
public:
bstNode();
bstNode(const Item& init_data, const Key& init_key, bstNode<Item, Key> *init_left, bstNode<Item, Key> *init_right);
~bstNode();
bstNode<Item, Key>* tree_copy(const bstNode<Item, Key>*& root);
private:
Item data;
Key key;
bstNode* left;
bstNode* right;
};
template <class Item, class Key>
//line 83 in the original code is below
bstNode<Item, Key>* bstNode<Item, Key>::tree_copy(const bstNode<Item, Key>*& root)
{
bstNode<Item, Key>* l_ptr;
bstNode<Item, Key>* r_ptr;
if (root == NULL) return NULL;
l_ptr = tree_copy(root -> left());
r_ptr = tree_copy(root -> right());
return new bstNode<Item, Key> (root -> data(), l_ptr, r_ptr);
}
.h 文件使用空的主函数编译良好,但是当我在 bstNode.cxx 中使用以下代码尝试它时,它崩溃了,给出了错误。代码为:
#include <cstddef>
#include <algorithm>
#include <math.h>
#include <iostream>
#include "bstNode.h"
using namespace std;
int main()
{
bstNode<int, size_t>* root_ptr = NULL;
bstNode<int, size_t>* copy_root_ptr = root_ptr -> tree_copy(root_ptr);
return 0;
}
错误是:
bstNode.cxx: In function ‘int main()’:
bstNode.cxx:14: error: no matching function for call to ‘bstNode<int, long unsigned int>::tree_copy(bstNode<int, long unsigned int>*&)’
bstNode.h:83: note: candidates are: bstNode<Item, Key>* bstNode<Item, Key>::tree_copy(const bstNode<Item, Key>*&) [with Item = int, Key = long unsigned int]
原型与函数的实现完全相同,没有bstNode::所以我不确定发生了什么。我正在使用 g++ 编译器。有什么想法吗?非常感谢,谢谢。
编辑:我减少了代码以尝试突出显示问题。
编译器(在大多数情况下(拒绝代码是正确的。问题是没有从T*&
到const T*&
的转换,所以现有的函数不能使用。
为什么不存在这种转换?
这种转换不存在的原因是它会破坏常量正确性。请考虑以下示例:
const int k = 10;
void f( const int*& kp ) {
kp = &k; // Fine, the pointer promises not to change the object
}
int main() {
int *p;
f( p ); // Does not compile, but assume it would
// after the call, p points to k
*p = 20; // Modifying a constant!!!!
// p never promised not to change the pointee
}
现在,由于您不需要修改传递给函数的指针,因此一个可能的解决方案是向签名添加更多const
:
bstNode<Item, Key>* tree_copy(const bstNode<Item, Key>* const & root);
这样做会阻止代码更改指针,这是上面示例中的问题。但是,如果你真的考虑一下,
为什么要首先传递对指针的引用?
指针的复制成本很低,因此按const&
传递指针没有多大意义,并且由于您不需要该函数来更改要传递的指针,因此按值传递将既正确又可能更有效。
原型并不完全相同,因为存在const
差异。声明是
bstNode<Item, Key>* tree_copy(const bstNode<Item, Key>*& root);
(引用常量指针(而您将其称为
bstNode<int, size_t>* root_ptr;
tree_copy(root_ptr);
所以它得到了对非常量指针的引用。虽然你可以将foo *
传递给需要const foo *
的东西,但你不能通过引用需要const foo * &
的东西来传递foo *
。
相关文章:
- 有一个打印语句的函数是一种糟糕的编程实践吗
- VSCode-有一个红色下划线,但程序构建和运行正确,并且出现配音错误
- 如果有一个模板构造函数只有一个泛型参数,为什么我必须有一个复制构造函数
- 有没有可能有一个只有ADL才能找到的非好友功能
- 我有一个对象,它将在整个程序的持续时间内实例化,但一个类成员不会,我应该动态分配它吗?
- 我的程序有一个保存配置文件的GUI,如何双击此配置文件以直接加载带有配置数据的GUI?
- 在学习数据结构之前对STL有一个了解是好的吗?
- 我在 .h 中有一个枚举类,并且在.cpp错误中有一个运算符重载:与"运算符<<不匹配
- 如何在 Gnuplot 中分别绘制 2 个文件数据?我有一个文件"sin.txt",另一个文件"cos.txt",我想将它们分别绘制在一个图表上
- 是否可以在C++中有一个"generic"模板参数,该参数可以是非类型模板参数或类型?
- 我的输出中有一个额外的 0,为什么会这样
- 节点是否为空,即使它有一个值?
- 将数组信息存储到 c++ 向量中有一个"Access violation reading location"
- 在 Stream C++ 文本之前有一个额外的换行符
- 我可以有一个 ELI5 作为参考和指针以及何时使用它们吗?
- 在 C++ 中声明 const 对象需要用户定义的默认构造函数.如果我有一个可变成员变量,为什么不呢?
- 函数在类中声明时应该有一个原型
- 将多个原型文件包含在一个项目中会导致protobuf_AssignDescriptorsOnce()已经有一个主体
- 编译器声明调用没有匹配的函数,即使原型中有一个
- 我不能有一个将多个整数作为参数的构造函数方法/原型吗?阿杜伊诺