error LNK2019 大神们帮我看看哪里错了吧,蟹蟹

Xiao_Bai_1996 2016-07-29 09:23:45
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 栈
...全文
155 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
Xiao_Bai_1996 2016-07-30
  • 打赏
  • 举报
回复
谢谢大家明白啦
小灸舞 2016-07-29
  • 打赏
  • 举报
回复
因为模板仅在需要的时候才会具现化出来。在我编写的模板cpp里头是没有具化的只有其定义,因此编译此单元并不会

生成相应的函数二进制代码。这就导致了在链接阶段会在其他调用模板单元函数时报未定义的错误。


因此把模板cpp代码全部放到.h中,问题就解决了。
paschen 版主 2016-07-29
  • 打赏
  • 举报
回复
模板类不支持分离式编译,把类的声明与实现放到同一个文件里 具体原因详见:http://blog.csdn.net/nestler/article/details/38731021
lm_whales 2016-07-29
  • 打赏
  • 举报
回复
带模板的.cpp不能单独编译,所以还要有个.cpp 包含它再编译 最好的方式是,谁用谁包含 另一种方式是显式实例化模板,不过这么用不符合使用模板的本意 使用模板主要是希望编译器自动生成代码 显式实例化,要显示声明模板类,有点多此一举的感觉 分离模板,好处不大, 如果不是模板代码很庞大,最好不分离。

64,685

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧