Squ*_*ama 14 c++ queue linker templates linker-errors
[[UPDATE]] - >如果我在program.cpp中#include"Queue.cpp",它就可以了.这不应该是必要的,对吗?
嘿所有 - 我正在使用Visual Studio 2010并且无法链接快速和脏的Queue实现.我从一个空的 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
Run Code Online (Sandbox Code Playgroud)
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;
}
}
Run Code Online (Sandbox Code Playgroud)
program.cpp
#include "Queue.hpp"
int main()
{
Queue<int> steve;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
而错误......
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_2\Project2_2\program.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_2\Project2_2\program.obj Project2_2
Run Code Online (Sandbox Code Playgroud)
解决错误lnk2019的一个例子:
它必须在.h文件的末尾写#include "EXAMPLE.cpp"
//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
//----------------------------------------------
Run Code Online (Sandbox Code Playgroud)
在 .cpp 文件中执行以下操作
//precompile header
#include "stdafx.h"
#pragma once
#ifndef _EXAMPLE_CPP_
#define _EXAMPLE_CPP_
template <class T>
void EXAMPLE<T>::Fnuction1()
{
//codes
}
#endif
//-----------------------------------------------
Run Code Online (Sandbox Code Playgroud)
如果你有:
template <typename T>
void foo();
Run Code Online (Sandbox Code Playgroud)
你也是:
foo<int>();
Run Code Online (Sandbox Code Playgroud)
编译器需要生成(实例化)该函数。但它不能这样做,除非函数定义在实例化点可见。
这意味着模板定义需要以某种方式包含在标题中。(您可以.cpp在标题的末尾包含 ,或仅提供内联定义。)
| 归档时间: |
|
| 查看次数: |
19053 次 |
| 最近记录: |