推导模板参数
Deducing template arguments
本文关键字:参数 更新时间:2023-10-16
我写了代码来构建二叉搜索树,也写了一个bfs二叉搜索树来测试它是否有效,但是我得到了很多错误,我调试了很长时间也找不到错误。
#include<vector>
template<typename Key, typename Value>
struct BSTNode{
Key key;
Value value;
BSTNode * left;
BSTNode * right;
BSTNode(Key k, Value v, BSTNode *l=NULL, BSTNode *r=NULL) :
key(k), value(v), left(l), right(r) {}
};
template<typename Key, typename Value>
BSTNode<Key, Value>* min_height(std::vector<int> &v, int left, int right ) {
if(left<=right){
int mid=left+ (right-left)/2;
BSTNode<Key, Value>* node=new BSTNode<Key, Value>(v[mid], v[mid]);
node->left=min_height(v, left, mid-1 );
node->right=min_height(v, mid+1, right );
return node;
}
}
int main(){
std::vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
BSTNode<int, int>* root=min_height(v, 0, 5);
}
编译错误:
prog.cpp: In function ‘BSTNode<Key, Value>* min_height(std::vector<int>&, int, int)’: prog.cpp:48:42: error: no matching function for call to ‘min_height(std::vector<int>&, int&, int&)’ prog.cpp:48:42: note: candidate is: prog.cpp:44:22: note: template<class Key, class Value> BSTNode<Key, Value>* min_height(std::vector<int>&, int, int) prog.cpp:44:22: note: template argument deduction/substitution failed: prog.cpp:48:42: note: couldn't deduce template parameter ‘Key’ prog.cpp:49:41: error: no matching function for call to ‘min_height(std::vector<int>&, int&, int&)’ prog.cpp:49:41: note: candidate is: prog.cpp:44:22: note: template<class Key, class Value> BSTNode<Key, Value>* min_height(std::vector<int>&, int, int) prog.cpp:44:22: note: template argument deduction/substitution failed: prog.cpp:49:41: note: couldn't deduce template parameter ‘Key’ prog.cpp: In function ‘int main()’: prog.cpp:63:45: error: no matching function for call to ‘min_height(std::vector<int>&, int, int)’ prog.cpp:63:45: note: candidate is: prog.cpp:44:22: note: template<class Key, class Value> BSTNode<Key, Value>* min_height(std::vector<int>&, int, int) prog.cpp:44:22: note: template argument deduction/substitution failed: prog.cpp:63:45: note: couldn't deduce template parameter ‘Key’ prog.cpp:63:22: warning: unused variable ‘root’ [-Wunused-variable]
不能根据返回类型推断模板类型。
BSTNode<int, int>* root = min_height<int, int>(v, 0, 5);
为了使其工作,您还需要更新您的函数,使其在调用min_height
时显式:
node->left = min_height<Key, Value>(v, left, mid - 1);
node->right = min_height<Key, Value>(v, mid + 1, right);
然而,从表面上看,min_height
总是将整数放入Key
和Value
。您可以通过使用某些Type
的vector
来使其更通用,并且通过使用该类型作为Key
和Value
类型来更显式。
template<typename Type>
BSTNode<Type, Type>* min_height(vector<Type> &v, int left, int right ) {
if(left<=right) {
int mid = left + (right - left)/2;
BSTNode<Type, Type>* node = new BSTNode<Type, Type>(v[mid], v[mid]);
node->left = min_height(v, left, mid - 1);
node->right = min_height(v, mid+1, right);
return node;
}
}
用法:
BSTNode<int, int>* root = min_height(v, 0, 5);
相关文章:
- 如何反转整数参数包
- 使用C++库在Android项目中修改gradle中的cmake参数,用于插入指令的测试
- 如何使用默认参数等选择模板专业化
- 模板参数替换失败,并且未完成隐式转换
- 具有默认模板参数的多态类的模板推导失败
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 函数调用中参数的顺序重要吗
- 部分定义/别名模板模板参数
- 模板-模板参数推导:三个不同的编译器三种不同的行为
- 使用不带参数的函数访问结构元素
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- 如何在OMNET++中指定与命令行参数组合的输出文件名
- 如何使用Luacneneneba API正确读取字符串和表参数
- 在派生函数中指定void*参数
- 视图中的参数推导失败:take_while
- static_assert在宏中,但也可以扩展到可以用作函数参数的东西
- 使用指向成员的指针将成员函数作为参数传递
- 没有名称的C++模板参数
- 如何将enable-if与模板参数和参数包一起使用