我正在学习C++,现在我正在使用Template。模板类型未定义
我试图实现链表:
ListElement.hpp
#ifndef LIST_ELEMENT_HPP_
#define LIST_ELEMENT_HPP_
template <class Type> class SingleLinkedList;
template <class Type>
class ListElement
{
public:
ListElement(const Type element);
~ListElement(void);
public:
Type val;
ListElement* next;
};
#endif
ListElement.cpp:
#include "ListElement.hpp"
ListElement<Type>::ListElement(const Type element)
{
*next = NULL;
val = element;
}
ListElement<Type>::~ListElement(void)
{
}
我得到的错误ListElement.cpp相对于Type
:Type is undefined
。
我发现了很多关于如何实现链表但没有使用分离的hpp和cpp的例子。
你知道我该如何解决这个错误?
最后的代码(类和实施预处理之后)都有,如果你不使用特定的实例出现在同一个文件实施。 – chris
不使用单独的源文件和头文件?当你使用模板时,它并不真正起作用,因为你的模板类的用户需要_whole_类定义,并且包含了这些函数。 –
另外,你在构造函数中初始化'next'不会达到你期望的效果!它将'NULL'分配给_random_位置。你想'next = nullptr;'(或'next = 0;')将_pointer_设置为NULL。 –