2010-09-14 47 views
14

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

正如上的代碼一般說明;你不需要爲頭上的每個班級都包括警衛,而且這樣做更有可能發生衝突。相反,根據該標題的文件名,在整個標題周圍放置一個包括守衛。這既是標準做法,也很少會遇到其他文件名。 – 2010-09-14 02:49:23

+0

重複的[Linker Error LNK2019](http://stackoverflow.com/questions/3680312/linker-error-lnk2019)(每週詢問這個問題五次) – 2010-09-14 03:37:56

回答

13

爲什麼不按照"Inclusion Model"?我建議你遵循這個模型。 編譯器需要訪問整個模板定義(不僅僅是簽名),以便爲模板的每個實例生成代碼,因此您需要將函數的定義移動到標題中。

注意:一般來說,大多數C++編譯器不會輕易支持模板的單獨編譯模型。

此外,你需要閱讀this.

4

鏈接器錯誤是因爲它看到Queue.hpp的頭文件,但沒有看到函數的定義。這是因爲它是一個模板類,對於C++,模板的定義必須位於頭文件中(還有其他一些選項,但這是最簡單的解決方案)。將函數的定義從Queue.cpp移到Queue.hpp,它應該被編譯。

+0

是的,所有文件都包含在內。例如,如果我在Queue.cpp中省略了分號,那麼項目不會進入鏈接過程,因爲它突然無法編譯。 – Squirrelsama 2010-09-14 02:38:31

+0

我沒有注意到在我第一次傳球時這是一個模板;這解釋了錯誤。 – 2010-09-14 02:39:07

+0

模板爲什麼會導致此錯誤?另外... [[UPDATE]] - >如果我#include「Queue.cpp」在我的program.cpp中,它工作得很好。這不應該是必要的,對嗎? – Squirrelsama 2010-09-14 02:41:28

4

編譯期間需要訪問所有模板代碼。將Queue.cpp實現細節移到標題中,以使它們可用。

4

如果您有:

template <typename T> 
void foo(); 

而你做的事:

foo<int>(); 

編譯器需要生成(實例)該功能。但它不能這樣做,除非函數定義在實例化處可見

這意味着模板定義需要以某種方式包含在標題中。 (可以包括.cpp在報頭的末尾,或者只是提供了內嵌的定義。)

2

解決錯誤LNK2019一個例子:
它必須寫的#include「EXAMPLE.cpp」在結束。.h文件

//header file codes 
#pragma once 
#ifndef EXAMPLE_H 
#define EXAMPLE_H 

template <class T> 
class EXAMPLE 
{ 

//class members 
void Fnuction1(); 

}; 


//write this 
#include "EXAMPLE.cpp" 


#endif 
//---------------------------------------------- 

在.cpp文件中做如下

//precompile header 
#include "stdafx.h" 
#pragma once 
#ifndef _EXAMPLE_CPP_ 
#define _EXAMPLE_CPP_ 

template <class T> 
void EXAMPLE<T>::Fnuction1() 
{ 
//codes 
} 

#endif 
//----------------------------------------------- 
相關問題