开发者

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


Try to move all the member function template definitions from TemplatedClass.cpp to TemplateClass.hh. All the template definitions should be in header files (simplified).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜