[英]How to use a pointer to a templated type
I apologize for the beginner nature of this question. 对于这个问题的初学者性质,我深表歉意。 I can't seem to get a pointer to a templated type to work.
我似乎无法获得指向模板化类型的指针。 For example, here is a header file:
例如,这是一个头文件:
#include "TemplatedClass.h"
using namespace std;
class PointList
{
public:
PointList(TemplatedClass<Point> *p);
//snip
};
This doesn't seem to be legal. 这似乎不合法。 What have I done wrong?
我做错了什么? Thanks!
谢谢!
edit adding to example. 编辑添加到示例。
//TemplatedClass.h
template <class T>
class TemplatedClass
{
public:
TemplatedClass();
TemplatedClass(T *t);
TemplatedClass(const TemplatedClass &t);
~TemplatedClass();
T *getNode() const;
void setNode(T *t);
private:
T *node;
};
//TemplatedClass.cpp
#include "TemplatedClass.h"
template <class T>
TemplatedClass<T>::TemplatedClass()
{
node = 0;
};
template <class T>
TemplatedClass<T>::TemplatedClass(T *t)
{
node = t;
};
template <class T>
TemplatedClass<T>::TemplatedClass(const TemplatedClass &l)
{
// snip
};
You need to provide the definition of the Point class by including a header where that class is defined. 您需要通过在其中定义该类的标头来提供Point类的定义。 For example :
例如 :
#include "TemplatedClass.h"
#include "Point.h" // if this header contains the Point's declaration
using namespace std;
class PointList
{
public:
PointList(TemplatedClass<Point> *p);
//snip
};
EDIT 编辑
Actually, since your method takes a pointer, you can just forward declare : 实际上,由于您的方法采用了指针,因此您可以向前声明:
template< typename T > class TemplatedClass;
class Point;
using namespace std;
class PointList
{
public:
PointList(TemplatedClass<Point> *p);
//snip
};
but then in the source (cpp) file you need to include those headers 但是在源(cpp)文件中,您需要包括这些标头
Try to move all the member function template definitions from TemplatedClass.cpp to TemplateClass.hh. 尝试将所有成员函数模板定义从TemplatedClass.cpp移至TemplateClass.hh。 All the template definitions should be in header files (simplified).
所有模板定义都应该在头文件中(简化)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.