错误LNK2019致命错误 C

ERROR LNK2019 FATAL ERROR C

本文关键字:致命错误 LNK2019 错误      更新时间:2023-10-16

可能的重复项:
为什么模板只能在头文件中实现?
具有简单模板类的"未定义的符号"链接器错误

队列.h

#include<iostream>
using namespace std;
template <class t>
class queue {  
    public:  
        queue(int=10);  
        void push(t&);  
        void pop();  
        bool empty();    
    private:  
        int maxqueue;  
        int emptyqueue;  
        int top;  
        t* item;  
};  

队列.cpp

#include<iostream>
#include"queue.h"
using namespace std;
template <class t>
queue<t>::queue(int a){
    maxqueue=a>0?a:10;
    emptyqueue=-1;
    item=new t[a];
    top=0;
}
template <class t>
void queue<t>::push(t &deger){
    if(empty()){
        item[top]=deger;
        top++;
    }
    else
        cout<<"queue is full";
}
template<class t>
void queue<t>::pop(){
    for(int i=0;i<maxqueue-1;i++){
        item[i]=item[i+1];
    }
    top--;
    if(top=emptyqueue)
        cout<<"queue is empty";
}
template<class t>
bool queue<t>::empty(){
    if((top+1)==maxqueue)
        return false
    else
        return true 
}

主.cpp

#include<iostream>
#include<conio.h>
#include"queue.h"
using namespace std;
void main(){
    queue<int>intqueue(5);
    int x=4;
    intqueue.push(x);
    getch();
}

我已经使用模板创建了队列。编译器给出了此错误。我无法解决这个问题。

1>main.obj :错误LNK2019:函数_main中引用的未解析的外部符号"public: void __thiscall queue::p ush(int)" (?push@?$queue@H@@QAEXH@Z) 1>main.obj : 错误 LNK2019: 未解析的外部符号 "public: __thiscall queue::queue(int)" (??0?$queue@H@@QAE@H@Z) 在函数 _main 中引用 1>c:\users\pc\documents\visual Studio 2010\Projects\lab10\Debug\lab10.exe:致命错误LNK1120:2 个未解析的外部

编辑:解决方案在这里给出。

将所有与模板相关的队列实现放在头文件中。就像:为什么模板只能在头文件中实现?

模板类不能分为 .cpp 和 .h 文件,因为编译器需要实现的副本才能从模板生成所需的类。

您需要将队列的内容.cpp移动到队列中.cpp

相关文章: