关于C++模板

yumangmang 2008-06-06 09:45:17
大家好,我现在使用VC++2003学习C++,现在学到了模板这个地方,昨天一个作业老出错,总说无法解析什么什么的,后来把模板的方法和声明放到了同一个文件中就好了,我就郁闷了,这个为啥呢?难道我忽略了什么?
...全文
149 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
yumangmang 2008-06-06
  • 打赏
  • 举报
回复
代码来了:
QueueTP.h

#ifndef QUEUETP_H_
#define QUEUETP_H_

template<typename Type>
class QueueTP
{
struct Cell
{
Type content;
Cell* pointer;
};
enum{MAX = 100};
private:
Cell* P_start;
Cell* P_end;
int number;
int top;
public:
QueueTP(int t = MAX);
~QueueTP();
bool isempty()const;
bool isfull()const;
bool push(const Type & item);
bool pop(Type & item);
void Show()const;
};

#endif


QueueTP.cpp


#include<iostream>
#include"QueueTP.h"

using std::cout;
using std::endl;

//Position 1

template<typename Type>
QueueTP<Type>::QueueTP(int t)
{
P_start = NULL;
P_end = NULL;
number = 0;
top = t;
}

template<typename Type>
QueueTP<Type>::~QueueTP()
{
Cell* P_temp;
while(number > 0)
{
P_temp = P_start;
P_start = P_start->pointer;
delete P_temp;
number--;
}
}

template<typename Type>
bool QueueTP<Type>::isempty() const
{
return number == 0;
}

template<typename Type>
bool QueueTP<Type>::isfull() const
{
return number == top;
}

template<typename Type>
bool QueueTP<Type>::push(const Type &item)
{
if(number >= top)
return false;
//
Cell* add = new Cell;
//
if(add == NULL)
return false;
//
add->content = item;
add->pointer = NULL;
//
if(P_start == NULL)
P_start = add;
else
P_end->pointer = add;
P_end = add;
//
number++;
//
return true;
}

template<typename Type>
bool QueueTP<Type>::pop(Type &item)
{
if(number <= 0)
return false;
//
Cell* P_temp;
//
item = P_start->content;
P_temp = P_start;
P_start = P_start->pointer;
//
delete P_temp;
//
number--;
//
if(number == 0)
P_end = NULL;
//
return true;
}

template<typename Type>
void QueueTP<Type>::Show()const
{
Cell* P_show = NULL;
P_show = P_start;
for(int i = 0;i < number;i++)
{
cout<<i+1<<": "<<P_show->content<<endl;
P_show = P_show->pointer;
}
}
//Position 2


usequeuetp.cpp


#include<iostream>
#include"QueueTP.h"

int main()
{
QueueTP<char*> mm;
char* dd[10] = {"aaaaaaa","bbbbb",
"ccccccc","ddddd",
"eeeeee","fffffff",
"ggggg","fffff",
"hhhhh","iiiii"};
int i;
for(i=9;i>=0;i--)
{
mm.push(dd[i]);
}

mm.Show();

return 0;
}


错误是:
1>------ 已启动生成: 项目: QueueTP, 配置: Debug Win32 ------
1>正在编译...
1>QueueTP.cpp
1>正在链接...
1>usequeuetp.obj : error LNK2019: 无法解析的外部符号 "public: __thiscall QueueTP<char *>::~QueueTP<char *>(void)" (??1?$QueueTP@PAD@@QAE@XZ),该符号在函数 _main 中被引用
1>usequeuetp.obj : error LNK2019: 无法解析的外部符号 "public: void __thiscall QueueTP<char *>::Show(void)const " (?Show@?$QueueTP@PAD@@QBEXXZ),该符号在函数 _main 中被引用
1>usequeuetp.obj : error LNK2019: 无法解析的外部符号 "public: bool __thiscall QueueTP<char *>::push(char * const &)" (?push@?$QueueTP@PAD@@QAE_NABQAD@Z),该符号在函数 _main 中被引用
1>usequeuetp.obj : error LNK2019: 无法解析的外部符号 "public: __thiscall QueueTP<char *>::QueueTP<char *>(int)" (??0?$QueueTP@PAD@@QAE@H@Z),该符号在函数 _main 中被引用
1>E:\VC++STUDY\200806\CeShi\QueueTP\Debug\QueueTP.exe : fatal error LNK1120: 4 个无法解析的外部命令
1>生成日志保存在“file://e:\VC++STUDY\200806\CeShi\QueueTP\QueueTP\Debug\BuildLog.htm”
1>QueueTP - 5 个错误,0 个警告
========== 生成: 0 已成功, 1 已失败, 0 最新, 0 已跳过 ==========
taodm 2008-06-06
  • 打赏
  • 举报
回复
因为你没有google “模板分离编译模式”
donwmufromdying 2008-06-06
  • 打赏
  • 举报
回复
代码都没有?怎么诊断?
yumangmang 2008-06-06
  • 打赏
  • 举报
回复
非常感谢各位回复
关于export关键字,vc编译器说它保留了该关键字,但是不支持——狂晕一个!
google “模板分离编译模式”在网上搜索了一下,没找到好的说明,看来只能先把声明和实现写一块了。:(
yshuise 2008-06-06
  • 打赏
  • 举报
回复
ls是你孤陋寡闻吧。
jieao111 2008-06-06
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 yshuise 的回复:]
好像用一个关键字"export"能行。
[/Quote]
不要误人哦,baidu之后在来
yshuise 2008-06-06
  • 打赏
  • 举报
回复
好像用一个关键字"export"能行。
jieao111 2008-06-06
  • 打赏
  • 举报
回复
c++ primer
任何讲到模板的c++书都有这个知识点
晨星 2008-06-06
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 ysuliu 的回复:]
老的编译器本来就不支持模板类的声明和定义分开。
[/Quote]
新的编译器支持得很好吗?
哪个编译器能做到像非模板代码那样真正的“分离”呢?
snow1313113 2008-06-06
  • 打赏
  • 举报
回复
主要是编译器对模块解析的问题
上次还见过更诡异的。。。在cpp里面就算你把代码写得一大堆的语法错误也是可以通过编译的。。。。
coverallwangp 2008-06-06
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 ysuliu 的回复:]
老的编译器本来就不支持模板类的声明和定义分开。

[/Quote]
herman~~ 2008-06-06
  • 打赏
  • 举报
回复
模板分离编译模式

这个ms我都没看过
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 taodm 的回复:]
因为你没有google “模板分离编译模式”
[/Quote]
lz多注意一下这个
ysuliu 2008-06-06
  • 打赏
  • 举报
回复
老的编译器本来就不支持模板类的声明和定义分开。
VC2003不知道,建议还是都定义到头文件里。

64,654

社区成员

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

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