![](/img/trans.png)
[英]how to declare templated map::iterator within a templated class. following code Says ; expected when compiled
[英]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.