简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM