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

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

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 &lt;typename T&gt; class Tree{
public:
    T data;
    vector&lt;Tree&lt;T&gt;*&gt;children;
    Tree(T data){
        this-&gt;data=data;
    }
};
template &lt;typename T&gt; Tree&lt;T&gt;* inputtree(){
    int element;
    cout&lt;&lt;&quot;Enter Root Node&quot;&lt;&lt;endl;
    cin&gt;&gt;element;
    cout&lt;&lt;&quot;Enter Total Number of children of Root :&quot;&lt;&lt;endl;
    Tree&lt;T&gt; * temp=new Tree&lt;T&gt;(element);
    return temp;
}
int main(){
        Tree&lt;int&gt;*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 &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)都必须提供模板参数。要么显式提供此参数,要么从函数参数中推导出来。

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

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&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:

template &lt;typename T&gt;
class Tree {
public:
    // ...

    // injected class name: Tree refers to Tree&lt;T&gt; here
    static Tree* input() {
        return /* ... */;
    }
}

int main() {
    // using auto, we don&#39;t repeat ourselves
    auto root = Tree&lt;int&gt;::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 &lt;vector&gt;
#include &lt;iostream&gt;
using namespace std;

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

 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:

确定