簡體   English   中英

C ++:嘗試在模板化類中聲明和定義模板化類。 我的代碼有什么問題?

[英]C++: Trying to declare and define templated class within templated class. What is wrong with my code?

#ifndef SIMPLE_BST_HPP
#define SIMPLE_BST_HPP

#include <vector>

template <class T>
class SimpleBST {

public:

    SimpleBST(std::vector<T>);


    template <class T>
    class BSTNode
    {


    };

};


#endif

但是,編譯時出現此錯誤:

In file included from SimpleBST.cpp:1:0:
SimpleBST.hpp:14:12: error: declaration of ‘class T’
  template <class T>
            ^
SimpleBST.hpp:6:11: error:  shadows template parm ‘class T’
 template <class T>
           ^
In file included from main.cpp:1:0:
SimpleBST.hpp:14:12: error: declaration of ‘class T’
  template <class T>
            ^
SimpleBST.hpp:6:11: error:  shadows template parm ‘class T’
 template <class T>
           ^

有誰知道為什么我無法定義BSTNode? 我最終將使該節點變為私有,但是在這一點上,我只是試圖在SimpleBST中聲明一個類,該類可用作存儲二進制搜索樹元素的基本節點。

謝謝!

考慮到要創建的類,對於嵌套類使用類模板是沒有意義的。

您不希望SimpleBST<double>包含BSTNode<int>

使BSTNode成為SimpleBST<T>下的簡單嵌套類型。

template <class T>
class SimpleBST {

public:

    SimpleBST(std::vector<T>);

    class BSTNode
    {
    };

};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM