'T'不是参数'T'的有效模板类型参数

时间:2022-04-25 16:31:13

I've written a program to convert a sorted array to BST in C++ using classes. I'm getting the following errors:

我编写了一个程序来使用c++将排序数组转换为BST。我得到以下错误:

error C2143: syntax error : missing ';' before '*'

error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

error C2065: 'T' : undeclared identifier

error C2923: 'Binary_Search_Tree' : 'T' is not a valid template type argument for parameter 'T'

The following is my program:

以下是我的项目:

template <class T>
class Binary_Search_Tree {
public:
    struct Bst_Node {
        T value;
        Bst_Node* left;
        Bst_Node* right;
    };

    Bst_Node* root;

    Binary_Search_Tree();

    Bst_Node* sortedarraytobst(T* A, int start, int end);
};

template <class T> Binary_Search_Tree<T>::Binary_Search_Tree() {
    root = nullptr;
    std::cout << "Constructor called" << std::endl;
}

template <class T> Bst_Node* Binary_Search_Tree<T>::sortedarraytobst(T* A, int start, int end) {

    if(start > end)
        return NULL;

    int mid = start + (end - start)/2;
    Bst_Node* newnode = new Bst_Node();
    newnode->value = A[mid];
    newnode->left  = sortedarraytobst(A,start,mid-1);
    newnode->right = sortedarraytobst(A,mid+1,end);

    return newnode;
}

int main()
{
    Binary_Search_Tree<int> tree;
    int Array[10] = {1,2,5,6,8,9,12};
    int n = sizeof(Array)/sizeof(Array[0]);
    tree.root = tree.sortedarraytobst(Array,0,n-1);

    return 0;
}

Request help in this regard. I'm finding the usage of templates very confusing.

在这方面请求帮助。我发现模板的使用非常混乱。

2 个解决方案

#1


4  

In the return value of the sortedarraytobst implementation, you should replace Bst_Node* with typename Binary_Search_Tree<T>::Bst_Node*. When put as a return type, it is seen as "outside the class", so it can't parse Bst_Node*.

在sortedarraytobst实现的返回值中,应该用typename Binary_Search_Tree : Bst_Node*替换Bst_Node*。当作为返回类型放置时,它被视为“类之外”,因此不能解析Bst_Node*。

#2


2  

As Binary_Search_Tree<T>::Bst_Node is a dependent name, the definition:

由于Binary_Search_Tree ::Bst_Node是一个相关名称,定义为:

template <class T>
Bst_Node* Binary_Search_Tree<T>::sortedarraytobst(T* A, int start, int end)
{/**/}

should be replaced by

应该被

template <class T>
typename Binary_Search_Tree<T>::Bst_Node*
Binary_Search_Tree<T>::sortedarraytobst(T* A, int start, int end)
{/**/}

or (since C++11)

或(因为c++ 11)

template <class T>
auto Binary_Search_Tree<T>::sortedarraytobst(T* A, int start, int end)
-> Bst_Node*
{/**/}

#1


4  

In the return value of the sortedarraytobst implementation, you should replace Bst_Node* with typename Binary_Search_Tree<T>::Bst_Node*. When put as a return type, it is seen as "outside the class", so it can't parse Bst_Node*.

在sortedarraytobst实现的返回值中,应该用typename Binary_Search_Tree : Bst_Node*替换Bst_Node*。当作为返回类型放置时,它被视为“类之外”,因此不能解析Bst_Node*。

#2


2  

As Binary_Search_Tree<T>::Bst_Node is a dependent name, the definition:

由于Binary_Search_Tree ::Bst_Node是一个相关名称,定义为:

template <class T>
Bst_Node* Binary_Search_Tree<T>::sortedarraytobst(T* A, int start, int end)
{/**/}

should be replaced by

应该被

template <class T>
typename Binary_Search_Tree<T>::Bst_Node*
Binary_Search_Tree<T>::sortedarraytobst(T* A, int start, int end)
{/**/}

or (since C++11)

或(因为c++ 11)

template <class T>
auto Binary_Search_Tree<T>::sortedarraytobst(T* A, int start, int end)
-> Bst_Node*
{/**/}