英文:
Couldn't deduce template parameter while using dynamic object created with specified typename
问题
我已经学习了一段时间的树数据结构。我尝试使用模板来实现树。在调用某些输入函数时,我遇到了一个错误。这是我遇到错误的地方。
template <typename T> class Tree{
public:
T data;
vector<Tree<T>*> children;
Tree(T data){
this->data=data;
}
};
template <typename T> Tree<T>* inputtree(){
int element;
cout<<"输入根节点"<<endl;
cin>>element;
cout<<"输入根节点的子节点数量:"<<endl;
Tree<T> * temp=new Tree<T>(element);
return temp;
}
int main(){
Tree<int>*root;
root=inputtree();
}
注意:我没有完全完成inputtree()
函数,但即使没有其中的内容,它仍然无法正常工作。
我在template <typename T> Tree<T>* inputtree()
这一行遇到了错误。为什么会出现这个错误?是语法错误还是我犯了其他错误?
我尝试通过删除模板功能并将直接作为返回类型添加来解决问题。问题得以解决。但我想使用模板来实现我的代码。
英文:
I have been learning tree data structure for a while. I tried to implement tree using templates. I came up with an error while calling some function for input. This is the fragment where I am getting error.
template <typename T> class Tree{
public:
T data;
vector<Tree<T>*>children;
Tree(T data){
this->data=data;
}
};
template <typename T> Tree<T>* inputtree(){
int element;
cout<<"Enter Root Node"<<endl;
cin>>element;
cout<<"Enter Total Number of children of Root :"<<endl;
Tree<T> * temp=new Tree<T>(element);
return temp;
}
int main(){
Tree<int>*root;
root=inputtree();
}
Note: I didn't complete inputtree()
function completely but still it is not functional without it's content inside it.
I am getting error at line template <typename T> Tree<T>* inputtree()
.
Why am I getting this error. Is it syntactically wrong or I am making some other mistake.
I tried resolving it by removing the template feature and adding direct as return type. The issue was resolved. However I want my code to be implemented with template.
答案1
得分: 1
inputtree
是一个函数模板,而不是普通函数。它的所有模板参数(在这种情况下只有T
)都必须提供模板参数。要么显式提供此参数,要么从函数参数中推导出来。
在你的情况下,这是不可能的:
root = inputtree();
你正在调用inputtree
,但编译器不知道T
应该是什么类型。这不能从赋值语句的左边推断出来。
要解决这个问题,你需要这样调用:
root = inputtree<int>();
这会明确为模板类型参数T
提供了参数int
。
备选解决方案
你还可以编写一个static
成员函数:
template <typename T>
class Tree {
public:
// ...
// 注入的类名:这里的Tree指的是Tree<T>
static Tree* input() {
return /* ... */;
}
}
int main() {
// 使用auto,我们不需要重复指定类型
auto root = Tree<int>::input();
}
英文:
inputtree
is a function template, not a regular function. All of its template parameters (in this case just T
) must be given a template argument. Either this argument is provided explicitly, or it is deduced from the function arguments.
In your case, this is not possible:
root = inputtree();
You are calling inputtree
, but the compiler has no idea what the T
is supposed to be. This cannot be inferred from the left hand side of the assignment.
To solve this problem, you would have to call:
root = inputtree<int>();
This provides the argument int
for the template type parameter T
explicitly.
Alternative Solution
You could also write a static
member function:
template <typename T>
class Tree {
public:
// ...
// injected class name: Tree refers to Tree<T> here
static Tree* input() {
return /* ... */;
}
}
int main() {
// using auto, we don't repeat ourselves
auto root = Tree<int>::input();
}
答案2
得分: 1
我在我的机器上运行了这个程序,包括那些内容,它正常工作了。
#include <vector>
#include <iostream>
using namespace std;
不要忘记为你的树添加数据类型,就像这样:
root = inputtree<int>();
英文:
I ran this program on my machine with those includes and it worked just fine
#include <vector>
#include <iostream>
using namespace std;
don't forget to add the type of the data for your tree
like this
root = inputtree<int>();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论