[[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