C2783: 无法推断帮助程序函数的模板参数
C2783: Could not deduce template argument for a helper function
我有一个Node和BinaryTree类:
template<typename Elem>
struct Node {
Elem Key;
Node <Elem>* parent = nullptr;
Node <Elem>* left = nullptr;
Node <Elem>* right = nullptr;
Node(Elem k);
};
template<typename Elem>
Node<Elem>::Node(Elem k):Key{k}{}
template<typename Elem>
class BinaryTree{
public:
class iterator; //Node<Elem>*
iterator root;
void insert(Elem val);
void remove(iterator& z);
};
使用二进制树中的类迭代器实现为:
template<typename Elem>
class BinaryTree<Elem>::iterator{
public:
iterator();
iterator(Node<Elem>* p);
Node<Elem>* curr = nullptr;
iterator& parent(); //set curr = curr->right
iterator& left();
iterator& right();
void setparent(iterator& b);//sets this.curr->parent = b.curr->parent
void setleft(iterator& b);
void setright(iterator& b);
iterator& Parent(); //Creates a new iterator that points to curr->parent and returns a reference to that
iterator& Left();
iterator& Right();
Elem& operator *(); // returns curr->Key
bool operator ==(iterator& b);
bool operator !=(iterator& b);
void operator =(iterator& b);
};
我做了一个在BinaryTree<Elem>::remove(iterator& z)
函数中使用的最小函数,实现为:
template<typename Elem>
typename BinaryTree<Elem>::iterator & minimum(typename BinaryTree<Elem>::iterator & z) {
while(z.Left().curr != nullptr) {
z.left();
}
return z;
}
remove 函数在调用minimum()
时,将z.Right()
作为参数,给出错误C2783,指出:
"BinaryTree::iterator &minimum(BinaryTree::iterator &(':无法推断出 'Elem' 的模板参数">
remove(( 函数实现为:
template<typename Elem>
void BinaryTree<Elem>::remove(iterator& z) {
if (z.Left().curr == nullptr) {
transplant(*this, z, z.Right());//The z.Right() creates a new iterator on the heap, whose curr pointer points to z.curr->right
}
else if (z.Right().curr != nullptr) {
transplant(*this, z, z.Left());
}
else {
iterator y = minimum(z.Right()); //-> This gives the error C2783 and C2762 (no matching overloaded function found)
if (y.Parent() != z) {
transplant(*this, y, z.Right());
y.curr->right = z.curr->right;
y.Right().curr->parent = y.curr;
}
transplant(*this, z, y);
y.curr->left = z.curr->left;
y.curr->left->parent = y.curr;
}
}
在几种上下文中,模板函数的参数是不可推导的。::
左侧有一个模板参数(范围解析运算符(的情况就是这样一种情况。
在以下情况下,类型、模板和非类型值...不要参与模板参数推导,而是使用在其他地方推导或明确指定的模板参数。
- 嵌套名称说明符(范围解析运算符
::
左侧的所有内容(的类型,该类型是使用限定 id 指定的...
(来源(
因此,在您的示例中,函数参数中::
运算符左侧的所有内容都在非推导上下文中:
template<typename Elem>
typename BinaryTree<Elem>::iterator & minimum(
typename BinaryTree<Elem>::iterator & z
^^^^^^^^^^^^^^^^ ^^^^^^^^
non-deduced deduced
)
扣除失败仅仅是因为在这种情况下没有尝试扣除。
请注意,右侧没有任何可推断的内容。 但是,在此示例中有:
template <typename Elem>
void foo(std::vector<Elem> const &)
可以在此处执行Elem
的扣除,因为它不显示在::
运算符的左侧。
最简单的解决方法就是不在乎这是否是BinaryTree<Elem>::iterator
,只接受任何类型:
template <typename T>
T & minimum(T & z) { ... }
相关文章:
- static_assert在宏中,但也可以扩展到可以用作函数参数的东西
- C++中的高效循环缓冲区,它将被传递给C样式数组函数参数
- 当从函数参数中的临时值调用复制构造函数时
- 如何从"decltype()"获取函数参数的数量<funtion>?
- 如何将lambda作为模板类的成员函数参数
- 模板参数推导失败,函数参数/参数不匹配
- 如何在C++中将迭代器作为函数参数传递
- 将函数参数"const char*"转换为"std::string_view"是
- C++ 如何将数组值解压缩为函数参数
- 主函数参数的属性
- 具有两个间接寻址运算符 (C++) 的函数参数的用途
- "Warning: Comma within array index expression"但逗号分隔函数参数
- 如何定义在用作函数参数时工作的类模板的转换
- 将函数参数完美转发到函数指针:按值传递呢?
- 为什么我不能将引用作为 std::async 的函数参数传递
- 什么..(省略号)作为函数原型中唯一的函数参数,C++?
- 是否可以就地构造一个固定大小的数组作为函数参数?
- 接受模板作为函数参数
- 将成员函数作为构造函数参数调用时出错 "Variable is not a type name"
- Arduino 函数参数