我用VC编了一个控制台程序,其中定义了一个类模板但是怎么也不能编译通过请大家看一下

ptxq 2002-01-18 12:19:53
我用VC编了一个控制台程序,其中定义了一个类模板但是怎么也不能编译通过请大家看一下,源码如下:
// stack.h: interface for the stack class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_STACK_H__790FC986_A080_41A0_BA29_5B50E7058FBE__INCLUDED_)
#define AFX_STACK_H__790FC986_A080_41A0_BA29_5B50E7058FBE__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

const int MaxStackSize=50;

template <class T>
class stack
{
public:
stack();
//virtual ~stack();
void Push(const T &item);
T Pop(void);
void ClearStack(void);
T Peek(void)const;
int StackEmpty(void) const;
int StackFull(void) const;

private:
T stacklist[MaxStackSize];
int top;
};

#endif // !defined(AFX_STACK_H__790FC986_A080_41A0_BA29_5B50E7058FBE__INCLUDED_)


// stack.cpp: implementation of the stack class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "stack.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
template <class T>
stack<T>::stack()
{
top=-1
}
//template <class T>
//stack<T>::~stack()
//{

//}

template <class T>
void stack<T>::Push(const T &item)
{
if(top==MaxStackSize-1)
{
cerr<<"Stack overflow!"<<endl;
exit(1);
}

top++;
stacklist[top]=item;
}

template <class T>
T stack<T>::Pop(void)
{
T temp;

if(top==-1)
{
cerr<<"attempt to pop an empty stack!"<<endl;
exit(1);
}

temp=stacklist[top];

top--;
return temp;
}

template <class T>
void stack<T>::ClearStack(void)
{
top=-1;
}

template <class T>
T stack<T>::Peek(void) const
{
T temp;
if(top==-1)
{
cerr<<"Attempt to pop an empty stack!"<<endl;
exit(1);
}

temp=stacklist[top];
return temp;
}

template <class T>
int stack<T>::StackEmpty(void) const
{
if(top==-1)
return 1;
return 0;
}

template <class T>
int stack<T>::StackFull(void) const
{
if(top==MaxStackSize-1)
return 1;
return 0;
}



// count.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "stack.h"
#include <iostream.h>
int main(void)
{
char a,b,d,e;
stack<char> m_cChar;
m_cChar.Push(a);
m_cChar.Push(b);
d=m_cChar.Peek();
e=m_cChar.Pop();
cout<<"d="<<d<<"e="<<e<<endl;
return 0;
}
出现的错误如下

--------------------Configuration: count - Win32 Debug--------------------
Compiling...
count.cpp
Linking...
count.obj : error LNK2001: unresolved external symbol "public: char __thiscall stack<char>::Pop(void)" (?Pop@?$stack@D@@QAEDXZ)
count.obj : error LNK2001: unresolved external symbol "public: char __thiscall stack<char>::Peek(void)const " (?Peek@?$stack@D@@QBEDXZ)
count.obj : error LNK2001: unresolved external symbol "public: void __thiscall stack<char>::Push(char const &)" (?Push@?$stack@D@@QAEXABD@Z)
count.obj : error LNK2001: unresolved external symbol "public: __thiscall stack<char>::stack<char>(void)" (??0?$stack@D@@QAE@XZ)
Debug/count.exe : fatal error LNK1120: 4 unresolved externals
Error executing link.exe.
Creating browse info file...

count.exe - 5 error(s), 0 warning(s)
请大家帮帮我
...全文
73 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
eion 2002-01-18
  • 打赏
  • 举报
回复
把stack换成Stack看看
BirdWang 2002-01-18
  • 打赏
  • 举报
回复
头文件中 #include <afxtempl.h>
将函数定义移到头文件中,肯定行
tingya 2002-01-18
  • 打赏
  • 举报
回复
这个问题我也遇到过,你把头文件和cpp放在一起写成一个文件应该没有问题了。
liu_feng_fly 2002-01-18
  • 打赏
  • 举报
回复
把template的定义和声明放到一起,再试一下
ptxq 2002-01-18
  • 打赏
  • 举报
回复
把头文件和cpp放在一起,写到一起,成功了。谢谢!

16,551

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Creator Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

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