我正在尝试创建一个设置LinkedList根节点的函数。但是,当我运行以下代码时:
#include <iostream>
using namespace std;
template <typename K>
struct Node {
Node<K>* next;
const K value;
};
template <typename K>
Node<K>* root = NULL;
template <typename K>
void SetRoot(const K &key) {
Node<K> new_node = Node<K> {NULL, key};
root = &new_node;
}
int main(int argc, char *argv[])
{
Node<int> n1 = Node<int> {NULL, 48};
SetRoot(n1);
return 0;
}
我在
root = &new_node;
行收到此错误:error: missing template arguments before ‘=’ token root = &new_node;
但是,
new_node
确实具有struct Node
的所有预期参数。
最佳答案
root
是variable template,使用时需要指定模板参数。例如
root<K> = &new_node;
// ^^^ specifying K which is the template parameter of SetRoot
顺便说一句:
new_node
是一个本地对象,当离开SetRoot
时将被销毁。之后,root<K>
挂了起来。
关于c++ - C++模板错误: ‘=’ token 之前缺少模板参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61630821/