我需要为我写入类模板的整数转换一个链表类。我在构造函数和析构函数中遇到了嵌套在List类中的结构的问题,称为node。
布局:
template <typename T>
class List
{
public:
//Stuff that's not important to this question
private:
struct Node
{
Node(T value); // constructor
~Node(); // destructor
Node *next; // pointer to the next Node
T data; // the actual data in the node
static int nodes_alive; // count of nodes still allocated
};
};
实现:
template <typename T>
typename List<T>::Node::Node(T value)
{
data = value;
next = 0;
}
template <typename T>
typename List<T>::Node::~Node()
{
--nodes_alive;
}
错误:
预期&#39;;&#39;在声明结束时
typename List :: Node :: Node(T value)
在&#39; ::&#39;
之后需要标识符或模板IDtypename List :: Node :: ~Node()
预期&#39;〜&#39;之后的班级名称命名析构函数
typename List :: Node :: ~Node()
不确定这里发生了什么。我的实现是在头文件底部包含的单独文件中。任何帮助将不胜感激。
答案 0 :(得分:3)
很简单:摆脱typename
关键字。由于您正在编写构造函数/析构函数并且没有返回类型,因此不需要它。
template <typename T>
List<T>::Node::Node(T value)
{
data = value;
next = 0;
}
template <typename T>
List<T>::Node::~Node()
{
--nodes_alive;
}