2010-09-14 760 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 
//----------------------------------------------- 
相关问题