unresolved externals

mymyway 2009-03-14 05:07:06
哪里有错误呢?怎么老是通不过?


///头文件stackhpp.h

template <class Type>
class Stack{
public:
Stack(int s);
~Stack();
void push(Type &item);
void pop();
Type top();
bool empty() const;
bool isfull() const;
private:
const int maxstack;
int count;
Type* stack;
};


////实现文件stackhpp.cpp

#include "stdafx.h"
#include "stackhpp.h"
#include<iostream>
#include"process.h"
using namespace std;

template <class Type>
Stack<Type>::Stack(int s)
{
maxstack=s;
Type*element=new Type[maxstack];
count=0;
}

template <class Type>
Stack<Type>::~Stack()
{
delete []element;
}

template <class Type>
void Stack<Type>::push(Type &item)
{
if(count>=maxstack){
cout<<"can not push.";
exit(1);
}
else
element[count++]=item;
}

template <class Type>
void Stack<Type>::pop()
{
if(count==0)
cout<<"can not pop.";
else
count--;
}


template <class Type>
Type Stack<Type>::top()
{
if(count==0)
cout<<"no elemenet.";
else
return element[count-1];
}


template <class Type>
bool Stack<Type>::empty() const
{
if(count==0)
return true;
else
return false;
}

template <class Type>
bool Stack<Type>::isfull() const
{
if(count==maxstack)
return true;
else
return false;
}


////主函数
// stack22.cpp : 定义控制台应用程序的入口点。
//

//#include "stdafx.h"
#include "stackhpp.h"


int main()
{
Stack <int> aaa(5);
return 0;
}


...全文
240 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
export关键字支持模板定义和声明分开。
一般,最好把定义跟声明都放在头文件里。
mymyway 2009-03-14
  • 打赏
  • 举报
回复
不可以这样定义一个动态数组吗?
pengzhixi 2009-03-14
  • 打赏
  • 举报
回复
如果想分开编译的话,要使用export关键字,不过目前支持这个关键字的编译器不多。
chin_chen 2009-03-14
  • 打赏
  • 举报
回复
试试vc2008,貌似可以编译通过
pengzhixi 2009-03-14
  • 打赏
  • 举报
回复
将stackhpp.cpp 内容放到stackhpp.h里面
liumingrong 2009-03-14
  • 打赏
  • 举报
回复
模板的定义和实现放在一个文件里
一般编译器不支持模板分离编译

64,680

社区成员

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

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