有问题的项目是关于不同的端点彼此通信。端点发送事件(超出当前问题的范围),并可处理传入事件。每个事件在一个通用对象中表示如下:C++抽象类模板+特定于类型的子类=链接器故障
#pragma interface
... // some includes
template<typename T>
class Event
{
public:
Event(int senderId, Type type, T payload); // Type is an enum
Event(int senderId, Type type, int priority, T payload);
virtual ~Event();
virtual int getSenderId();
virtual int getPriority();
virtual T getPayload();
void setPriority(const int priority);
protected:
const int senderId;
const Type type;
const T payload;
int priority;
};
它具有带#pragma实现标记的实现类。
端点定义如下:
#pragma interface
#include "Event.h"
template<typename T>
class AbstractEndPoint
{
public:
AbstractEndPoint(int id);
virtual ~AbstractEndPoint();
virtual int getId();
virtual void processEvent(Event<T> event) = 0;
protected:
const int id;
};
它有它的实现类也一样,但只有构造函数,析构函数的getId()被定义。
这个想法是为每个不同的有效载荷类型创建具体的端点。因此,对于每种类型,我都有不同的有效载荷对象和特定事件类别。
Event<TelegramFormatA>, Event<TelegramFormatB>
和
ConcreteEndPoint for TelegramFormatA,
ConcreteEndPoint for TelegramFormatB
分别。后者类定义为
class ConcreteEndPoint : AbstractEndPoint<TelegramFormatA>
{
...
}
我使用的是g ++ 4.4.3和ld 2.19。一切都很好地编译,而是未定义的引用链接投诉键入特定的事件类,像
Event<TelegramFormatA>::Event(....) .
我使用
template class AbstractEndPoint<TelegramFormatA>;
试图显式实例,但无法摆脱上述链接错误。
任何想法,将不胜感激。
它可以在.cpp文件中实现,但它必须#included在头文件中,以便实现可见。它不能自行编译。按照这种方式组织时,通常会使用不同的文件扩展名(通常为.inc)。 – 2010-05-05 11:08:06