无法在使用指定类型名创建的动态对象时推导出模板参数。

huangapple go评论91阅读模式
英文:

Couldn't deduce template parameter while using dynamic object created with specified typename

问题

我已经学习了一段时间的树数据结构。我尝试使用模板来实现树。在调用某些输入函数时,我遇到了一个错误。这是我遇到错误的地方。

  1. template <typename T> class Tree{
  2. public:
  3. T data;
  4. vector<Tree<T>*> children;
  5. Tree(T data){
  6. this->data=data;
  7. }
  8. };
  9. template <typename T> Tree<T>* inputtree(){
  10. int element;
  11. cout<<"输入根节点"<<endl;
  12. cin>>element;
  13. cout<<"输入根节点的子节点数量:"<<endl;
  14. Tree<T> * temp=new Tree<T>(element);
  15. return temp;
  16. }
  17. int main(){
  18. Tree<int>*root;
  19. root=inputtree();
  20. }

注意:我没有完全完成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.

  1. template &lt;typename T&gt; class Tree{
  2. public:
  3. T data;
  4. vector&lt;Tree&lt;T&gt;*&gt;children;
  5. Tree(T data){
  6. this-&gt;data=data;
  7. }
  8. };
  9. template &lt;typename T&gt; Tree&lt;T&gt;* inputtree(){
  10. int element;
  11. cout&lt;&lt;&quot;Enter Root Node&quot;&lt;&lt;endl;
  12. cin&gt;&gt;element;
  13. cout&lt;&lt;&quot;Enter Total Number of children of Root :&quot;&lt;&lt;endl;
  14. Tree&lt;T&gt; * temp=new Tree&lt;T&gt;(element);
  15. return temp;
  16. }
  17. int main(){
  18. Tree&lt;int&gt;*root;
  19. root=inputtree();
  20. }

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 &lt;typename T&gt; Tree&lt;T&gt;* 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)都必须提供模板参数。要么显式提供此参数,要么从函数参数中推导出来。

在你的情况下,这是不可能的:

  1. root = inputtree();

你正在调用inputtree,但编译器不知道T应该是什么类型。这不能从赋值语句的左边推断出来。

要解决这个问题,你需要这样调用:

  1. root = inputtree<int>();

这会明确为模板类型参数T提供了参数int

备选解决方案

你还可以编写一个static成员函数:

  1. template <typename T>
  2. class Tree {
  3. public:
  4. // ...
  5. // 注入的类名:这里的Tree指的是Tree<T>
  6. static Tree* input() {
  7. return /* ... */;
  8. }
  9. }
  10. int main() {
  11. // 使用auto,我们不需要重复指定类型
  12. auto root = Tree<int>::input();
  13. }
英文:

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:

  1. 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:

  1. root = inputtree&lt;int&gt;();

This provides the argument int for the template type parameter T explicitly.

Alternative Solution

You could also write a static member function:

  1. template &lt;typename T&gt;
  2. class Tree {
  3. public:
  4. // ...
  5. // injected class name: Tree refers to Tree&lt;T&gt; here
  6. static Tree* input() {
  7. return /* ... */;
  8. }
  9. }
  10. int main() {
  11. // using auto, we don&#39;t repeat ourselves
  12. auto root = Tree&lt;int&gt;::input();
  13. }

答案2

得分: 1

我在我的机器上运行了这个程序,包括那些内容,它正常工作了。

  1. #include <vector>
  2. #include <iostream>
  3. using namespace std;

不要忘记为你的树添加数据类型,就像这样:

  1. root = inputtree<int>();
英文:

I ran this program on my machine with those includes and it worked just fine

  1. #include &lt;vector&gt;
  2. #include &lt;iostream&gt;
  3. using namespace std;

don't forget to add the type of the data for your tree
like this

  1. root = inputtree&lt;int&gt;();

huangapple
  • 本文由 发表于 2023年6月15日 00:25:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76475699.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定