soxarered
soxarered

Reputation: 163

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
};

Upvotes: 0

Views: 106

Answers (2)

BЈовић
BЈовић

Reputation: 64213

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

Upvotes: 3

Adam Trhon
Adam Trhon

Reputation: 2935

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

Upvotes: 0

Related Questions