简体   繁体   中英

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

However, I get this error when compiling:

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>
           ^

Does anyone know why I am unable to define the BSTNode? I will eventually make the node private, but at this point, I am just trying to declare a class within the SimpleBST that can be used as the basic node to store the elements of my binary search tree.

Thanks!

Considering the classes you are creating, it does not make sense that you are using a class template for the nested class.

You don't want SimpleBST<double> to contain BSTNode<int> .

Make BSTNode a simple nested type under SimpleBST<T> .

template <class T>
class SimpleBST {

public:

    SimpleBST(std::vector<T>);

    class BSTNode
    {
    };

};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM