C++ - LNK2019 error unresolved external symbol [template class#39;s constructor and destructor] referenced in function _main(C++ - LNK2019 错误未解析的外部符号 [模板类的构造函数和析构函数] 在函数 _main 中引用)
问题描述
[[UPDATE]] -> 如果我 #include "Queue.cpp" 在我的 program.cpp 中,它工作得很好.这应该没有必要吧?
[[UPDATE]] -> If I #include "Queue.cpp" in my program.cpp, it works just fine. This shouldn't be necessary, right?
大家好——我正在使用 Visual Studio 2010,但在链接快速而肮脏的队列实现时遇到了问题.我从一个 empty Win32 控制台应用程序开始,所有文件都存在于项目中.对于冗长,这里是复制我的错误的完整代码.我意识到某些代码实际上可能是错误的.我还没有机会测试它,因为我还不能链接它.
Hey all -- I'm using Visual Studio 2010 and having trouble linking a quick-and-dirty Queue implementation. I started with an empty Win32 Console Application, and all files are present in the project. For verbosity, here's the complete code to duplicate my error. I realize some of the code may, in fact, be wrong. I haven't had a chance to test it yet because I haven't yet been able to link it.
队列.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
队列.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;
}
}
程序.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)" (??1?$Queue@H@@QAE@XZ) referenced in function _main C:[omitted]Project2_2Project2_2program.obj Project2_2
Error 2 error LNK2019: unresolved external symbol "public: __thiscall Queue<int>::Queue<int>(void)" (??0?$Queue@H@@QAE@XZ) referenced in function _main C:[omitted]Project2_2Project2_2program.obj Project2_2
推荐答案
你为什么不关注 "包含模型"?我建议您遵循该模型.编译器需要访问整个模板定义(不仅仅是签名),以便为模板的每个实例化生成代码,因此您需要将函数的定义移动到您的头文件中.
Why don't you follow the "Inclusion Model"? I'd recommend you follow that model. The compiler needs to have access to the entire template definition (not just the signature) in order to generate code for each instantiation of the template, so you need to move the definitions of the functions to your header.
注意:通常大多数 C++ 编译器都不容易支持模板的单独编译模型.
Note: In general most C++ compilers do not easily support the separate compilation model for templates.
您还需要阅读this.
这篇关于C++ - LNK2019 错误未解析的外部符号 [模板类的构造函数和析构函数] 在函数 _main 中引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:C++ - LNK2019 错误未解析的外部符号 [模板类的构造函数和析构函数] 在函数 _main 中引用
基础教程推荐
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- CString 到 char* 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
