2011-07-17 80 views
1

我想用我的自定义模板链表类,并得到了链接错误来实现粒子系统:与模板类链接错误

Error 3 error LNK2019: unresolved external symbol "public: struct Node<class Particle> * __thiscall LinkedList<class Particle>::Pop(void)" ([email protected][email protected]@@@@[email protected]@@@@XZ) referenced in function "private: void __thiscall ParticleEmitter::addParticles(void)" ([email protected]@@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 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

[模板运算符链接程序错误](http://stackoverflow.com/questions/3007251/template-operator-linker-error)(和更多,但这个有一个很好的和“教诲”接受的答案) –

回答

3

您是否在源(例如.cpp)文件中定义了Pop函数,而不是头文件?你不能用模板这样做。您需要在头文件中提供函数定义。定义需要在实例化时可见。

+0

等待没有定义在CPP –

+0

@Mikhail:所以将它移动到标题。 –

+0

谢谢修复它! –