为什么会这样?为什么

老田低代码 2005-04-09 10:49:32
我现在正在学习C++,但是这些问题实在是很打击我哦。。。没有人旁边指导,日子。。苦!!!!
#include "tstack.h"

void main()
{
TStack<int> istack;
for(int ix=1;ix<10;ix++)
{
istack.push(ix);
}

istack.display();
}

出现的错误提示:
main.obj : error LNK2001: unresolved external symbol "public: void __thiscall TStack<int>::display(void)" (?display@?$TStack@H@@QAEXXZ)

main.obj : error LNK2001: unresolved external symbol "public: bool __thiscall TStack<int>::push(int)" (?push@?$TStack@H@@QAE_NH@Z)

Debug/TStack.exe : fatal error LNK1120: 2 unresolved externals

但是我在类实现中TStack.cpp
template <class T>
bool TStack<T>::push(T value)
{
if (_size==100)
return false;
_stacklist[++_top]=value;
_size=_size+1;
return true;
}

template <class T>
void TStack<T>::display()
{
for(in ix=0;ix<=_top;ix++)
cout<<_stacklist[ix]<<" ";
cout<<endl;
}

以及在类定义中:
template <class T>

class TStack
{
public:
TStack():_size(0),_top(0){}
bool pop(T &value);
bool push(T value);
void display();
private:
int _size;
int _top;
T _stacklist[100];
};
但是就是出现上面的提示,为什么??
...全文
120 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
sunandmoon1314 2005-04-10
  • 打赏
  • 举报
回复
chunhai12(苦行僧)厉害
老田低代码 2005-04-09
  • 打赏
  • 举报
回复
谢谢了各位:

应该就是chunhai12(苦行僧) 所说的模板类的申明和定义都要放在头文件中现在的编译器不支持模板类分离编译这样的原因!
xjp6688 2005-04-09
  • 打赏
  • 举报
回复
#include <iostream>
#include <stdlib.h>

#include<stdio.h>
#include <iostream>
#include <stdlib.h>
#include <stack>
using namespace std;


template <class T>

class TStack
{
public:
TStack():_size(0),_top(0){}
bool pop(T &value);
bool push(T value);
void display();
private:
int _size;
int _top;
T _stacklist[100];
};

template <class T>
bool TStack<T>::push(T value)
{
if (_size==100)
return false;
_stacklist[++_top]=value;
_size=_size+1;
return true;
}

template <class T>
void TStack<T>::display()
{
for(int ix=0;ix<=_top;ix++)
cout<<_stacklist[ix]<<" ";
cout<<endl;
}


int main()
{
TStack<int> istack;
for(int ix=1;ix<10;ix++)
{
istack.push(ix);
}

istack.display();
system("pause");
return 0;
}

这个在DEV4下调试成功!
结果
4198400 1 2 3 4 5 6 7 8 9
请按任意键继续. . .
xjp6688 2005-04-09
  • 打赏
  • 举报
回复
35 1 2 3 4 5 6 7 8 9
请按任意键继续. . .

这是结果!
xjp6688 2005-04-09
  • 打赏
  • 举报
回复
#include<stdio.h>
#include <iostream>
#include <stdlib.h>
#include <stack>
using namespace std;


template <class T>

class TStack
{
public:
TStack():_size(0),_top(0){}
bool pop(T &value);
bool push(T value);
void display();
private:
int _size;
int _top;
T _stacklist[100];
};

template <class T>
bool TStack<T>::push(T value)
{
if (_size==100)
return false;
_stacklist[++_top]=value;
_size=_size+1;
return true;
}

template <class T>
void TStack<T>::display()
{
for(int ix=0;ix<=_top;ix++)
cout<<_stacklist[ix]<<" ";
cout<<endl;
}


void main()
{
TStack<int> istack;
for(int ix=1;ix<10;ix++)
{
istack.push(ix);
}

istack.display();
system("pause");
}

在BCB6下调试成功!
MagicCarmack 2005-04-09
  • 打赏
  • 举报
回复
学习...
chunhai12 2005-04-09
  • 打赏
  • 举报
回复
问了N遍的问题了
模板类的申明和定义都要放在头文件中
现在的编译器不支持模板类分离编译
llf_hust 2005-04-09
  • 打赏
  • 举报
回复
你把头文件和实现写在一起看看呀,这种问题我也碰到过写在一起就没有事情了
老田低代码 2005-04-09
  • 打赏
  • 举报
回复
现在关键不是使用什么的问题,而是为什么这样的问题了!!!
lw1a2 2005-04-09
  • 打赏
  • 举报
回复
学习的话,不要用VC。用DevCPP吧

65,187

社区成员

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

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