error LNK2019 们帮本人看看哪里错了吧,蟹蟹

C++语言 码拜 8年前 (2016-05-16) 846次浏览
stack。h文件
#ifndef STACK_H_
#define STACK_H_
template<class Type>
class Stack
{
private:
enum
{
MAX=10
};
Type items[MAX];
int top;
public:
Stack();
bool isempty();
bool isfull();
bool push(const Type&item);//add item to stack
//returns false if stack already is full
bool pop(Type&item);// pop top into item
//returns false if stack already is empty
};
#endif

stack。cpp文件

#include”stack.h”
template<class Type>
Stack<Type>::Stack()
{
top = 0;
}
template<class Type>
bool Stack<Type>::isempty()
{
return top == 0;
}
template<class Type>
bool Stack<Type>::isfull()
{
return top == MAX;
}
template<class Type>
bool  Stack<Type>::push(const Type&item)
{
if (top < MAX)
{
items[top++] = item;
return true;
}
else
return false;
}
template<class Type>
bool  Stack<Type>::pop(Type&item)
{
if (top > 0)
{
item = items[–top];
return true;
}
else
return false;
}
stacker。cpp文件
#include “stack.h”
#include<iostream>
#include<cctype>
#include<string>
int  main()
{
Stack<std::string> st;
char ch;
std::string po;
std::cout << “请输入:a 添加||||||p删除|||||||q退出\n”;
while (std::cin >> ch&&toupper(ch) != “Q”)
{
while (std::cin.get() != “\n”)
continue;
if (!isalpha(ch))
{
std::cout << “\a\n” ;
continue;
}
switch (ch)
{
case “A”:
case “a”:std::cout << “enter a po number\n” ;
std::cin >> po;
if (st.isfull())
std::cout << “stack already full\n” ;
else
st.push(po);
break;
case “P”:
case “p”:if (st.isempty())
std::cout << “stack is empty\n” ;
else
{
st.pop(po);
std::cout << “po  #” << po << “popped\n”;
}
break;
}
std::cout << “请输入:a 添加||||||p删除|||||||q退出\n” ;
}
std::cout << “bye”;
return 0;
}
编译器的报错
错误 1 error LNK2019: 无法解析的外部符号 “public: bool __thiscall Stack<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >::isempty(void)” (?isempty@?$Stack@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@@QAE_NXZ),该符号在函数 _main 中被引用 C:\Users\Liaolin–Only\Desktop\空项目\栈\栈\stacker.obj 栈
错误 2 error LNK2019: 无法解析的外部符号 “public: bool __thiscall Stack<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >::isfull(void)” (?isfull@?$Stack@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@@QAE_NXZ),该符号在函数 _main 中被引用 C:\Users\Liaolin–Only\Desktop\空项目\栈\栈\stacker.obj 栈
错误 3 error LNK2019: 无法解析的外部符号 “public: bool __thiscall Stack<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >::push(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)” (?push@?$Stack@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@@QAE_NABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z),该符号在函数 _main 中被引用 C:\Users\Liaolin–Only\Desktop\空项目\栈\栈\stacker.obj 栈
错误 4 error LNK2019: 无法解析的外部符号 “public: bool __thiscall Stack<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >::pop(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > &)” (?pop@?$Stack@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@@QAE_NAAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z),该符号在函数 _main 中被引用 C:\Users\Liaolin–Only\Desktop\空项目\栈\栈\stacker.obj 栈
错误 5 error LNK1120: 4 个无法解析的外部命令 C:\Users\Liaolin–Only\Desktop\空项目\栈\Debug\栈.exe 栈
解决方案

5

带模板的.cpp不能单独编译,所以还要有个.cpp 包含它再编译
最好的方式是,谁用谁包含
另一种方式是显式实例化模板,不过这么用不符合使用模板的本意
使用模板主要是希望编译器自动生成代码
显式实例化,要显示声明模板类,有点多此一举的感觉
分离模板,好处不大,
假如不是模板代码很庞大,最好不分离。

20

模板类不支持分离式编译,把类的声明与实现放到同一个文件里
具体原因详见:http://blog.csdn.net/nestler/article/details/38731021

10

原因是模板仅在需要的时候才会具现化出来。在本人编写的模板cpp里头是没有具化的只有其定义,因此编译此单元并不会
生成相应的函数二进制代码。这就导致了在链接阶段会在其他调用模板单元函数时报未定义的错误。
因此把模板cpp代码全部放到.h中,问题就解决了。

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明error LNK2019 们帮本人看看哪里错了吧,蟹蟹
喜欢 (0)
[1034331897@qq.com]
分享 (0)