我正在尝试使用自定义模板链接列表实现粒子系统并获取链接器错误:
Error 3 error LNK2019: unresolved external symbol "public: struct Node<class Particle> * __thiscall LinkedList<class Particle>::Pop(void)" (?Pop@?$LinkedList@VParticle@@@@QAEPAU?$Node@VParticle@@@@XZ) referenced in function "private: void __thiscall ParticleEmitter::addParticles(void)" (?addParticles@ParticleEmitter@@AAEXXZ)*
这是Node.h:
template <class T>
struct Node
{
Node(const T&);
~Node();
T* value;
Node* previous;
Node* next;
};
这是addParticle代码:
LinkedList<Particle> freeParticles; //definition in ParticleEmitter.h
void ParticleEmitter::addParticles(void)
{
int numParticles = RandomGetInt(minParticles, maxParticles);
for (int i = 0; i < numParticles && !freeParticles.IsEmpty(); i++)
{
// grab a particle from the freeParticles queue, and Initialize it.
Node<Particle> *n = freeParticles.Pop();
n->value->init(color,
position,
direction * RandomGetFloat(velMin, velMax),
RandomGetFloat(lifetimeMin, lifetimeMax));
}
}
这是Pop功能:
//Pop from back
template <class T>
Node<T>* LinkedList<T>::Pop()
{
if (IsEmpty()) return NULL;
Node<T>* node = last;
last = last->previous;
if (last != NULL)
{
last->next = NULL;
if (last->previous == NULL) head = last;
}
else head = NULL;
node->previous = NULL;
node->next = NULL;
return node;
}
我正在从头开始编写所有代码,所以也许我在某处犯了一个错误,我也是模板的新手。
答案 0 :(得分:3)
您是否在源(例如.cpp)文件中定义了Pop函数,而不是头文件?你不能用模板这样做。您需要在头文件中提供函数定义。定义需要在实例化时可见。