C ++ - LNK2019错误未解析的外部符号[模板类的构造函数和析构函数]在函数_main [英] C++ - LNK2019 error unresolved external symbol [template class's constructor and destructor] referenced in function _main

查看:547
本文介绍了C ++ - LNK2019错误未解析的外部符号[模板类的构造函数和析构函数]在函数_main的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[[UPDATE]] - >如果我#includeQueue.cpp在我的program.cpp,它的工作就好了。这不应该是必要的,对吗?



嘿,所有 - 我使用Visual Studio 2010和麻烦的链接一个快速和脏的队列实现。我开始使用空白 Win32控制台应用程序,所有文件都存在于项目中。对于详细程度,这里是完整的代码复制我的错误。我意识到一些代码可能,实际上,是错误的。我还没有机会测试它,因为我还没能链接它。



Queue.hpp

  #ifndef ERROR_CODE 
#define ERROR_CODE
enum Error_Code
{
成功,
下溢,
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> *前,*后;
};
#endif // QUEUE

Queue.cpp

  #includeQueue.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溢出;

if(this-> rear!= NULL)
this-> rear-> next = temp;
this-> rear = temp;

return成功;
}

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-& (entry-> next);
delete entry;
}
}

program.cpp

  #includeQueue.hpp

int main()
{
Queue< int>史蒂夫
return 0;
}

错误...

 错误1错误LNK2019:未解析的外部符号public:__thiscall Queue< int> ::〜Queue< int>(void) @@ QAE @ XZ)在函数_main中引用C:\ [omitted] \Project2_2\Project2_2\program.obj Project2_2 
错误2错误LNK2019:未解析的外部符号public:__thiscall Queue< int> ;: :队列< INT>(无效)(?? 0 $队列@ H一般@@ QAE @ XZ?)函数_mainç引用:\ [省略] \Project2_2\Project2_2\program.obj Project2_2


解决方案

你为什么不跟着的包含模式?我建议你按照那个模型。
中的编译器需要访问,以便产生用于模板的每个实例化代码整个模板定义(不只是签名),所以需要的函数的定义移动到头部。

请注意:通常,大部分C ++编译器不会轻易支持模板的独立编译模型



此外,你需要阅读的 this。


[[UPDATE]] -> If I #include "Queue.cpp" in my program.cpp, it works just fine. This shouldn't be necessary, right?

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.

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;
}

And the errors...

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

解决方案

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.

Note: In general most C++ compilers do not easily support the separate compilation model for templates.

Furthermore you need to read this.

这篇关于C ++ - LNK2019错误未解析的外部符号[模板类的构造函数和析构函数]在函数_main的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆