[[UPDATE]] - >如果我#include“Queue.cpp”在我的program.cpp,它工作得很好。这不应该是必要的,对吗?C++ - LNK2019错误无法解析的外部符号[模板类的构造函数和析构函数]在函数_main中引用
嘿所有 - 我使用的是Visual Studio 2010,并且无法连接快速和肮脏的队列实现。我开始使用空的 Win32控制台应用程序,并且项目中存在所有文件。详细信息,这里是完整的代码来复制我的错误。我意识到一些代码实际上可能是错误的。我还没有机会测试它,因为我还没有能够链接它。
Queue.hpp
#ifndef ERROR_CODE
#define ERROR_CODE
enum Error_Code
{
Success,
Underflow,
Overflow
};
#endif // ERROR_CODE
#ifndef QUEUE
#define QUEUE
template<class T>
struct Queue_Node
{
T data;
Queue_Node *next;
Queue_Node()
{
next = NULL;
}
Queue_Node(T pData)
{
data = pData;
next = NULL;
}
Queue_Node(T pData, Queue_Node *pNext)
{
data = pData;
next = pNext;
}
};
template<class T>
class Queue
{
public:
Queue();
Error_Code Serve();
Error_Code Append(T item);
T Front();
~Queue();
private:
void Rescursive_Destroy(Queue_Node<T> *entry);
Queue_Node<T> *front, *rear;
};
#endif // QUEUE
Queue.cpp
#include "Queue.hpp"
template <class T>
Queue<T>::Queue()
{
this->front = this->rear = NULL;
}
template<class T>
Error_Code Queue<T>::Serve()
{
if(front == NULL)
return Underflow;
Queue_Node *temp = this->front;
this->front = this->front->next;
delete temp;
}
template<class T>
Error_Code Queue<T>::Append(T item)
{
Queue_Node *temp = new Queue_Node(item);
if(!temp)
return Overflow;
if(this->rear != NULL)
this->rear->next = temp;
this->rear = temp;
return Success;
}
template<class T>
T Queue<T>::Front()
{
if(this->front == NULL)
return Underflow;
return this->front->data;
}
template<class T>
Queue<T>::~Queue()
{
this->Rescursive_Destroy(this->front);
}
template<class T>
void Queue<T>::Rescursive_Destroy(Queue_Node<T> *entry)
{
if(entry != NULL)
{
this->Recursive_Destroy(entry->next);
delete entry;
}
}
program.cpp
#include "Queue.hpp"
int main()
{
Queue<int> steve;
return 0;
}
而且错误...
Error 1 error LNK2019: unresolved external symbol "public: __thiscall Queue<int>::~Queue<int>(void)" ([email protected]@@[email protected]) referenced in function _main C:\[omitted]\Project2_2\Project2_2\program.obj Project2_2
Error 2 error LNK2019: unresolved external symbol "public: __thiscall Queue<int>::Queue<int>(void)" ([email protected]@@[email protected]) referenced in function _main C:\[omitted]\Project2_2\Project2_2\program.obj Project2_2
正如上的代码一般说明;你不需要为头上的每个班级都包括警卫,而且这样做更有可能发生冲突。相反,根据该标题的文件名,在整个标题周围放置一个包括守卫。这既是标准做法,也很少会遇到其他文件名。 – 2010-09-14 02:49:23
重复的[Linker Error LNK2019](http://stackoverflow.com/questions/3680312/linker-error-lnk2019)(每周询问这个问题五次) – 2010-09-14 03:37:56