我用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)
请大家帮帮我