写了个程序分为三个文件SeqStack.h(顺序栈),MembFunction.cpp(成员函数),Main.cpp编译时提示
例如 [Linker error] undefined reference to `SeqStack<int>::IsEmpty() const'
#include"SeqStack.h"
#include<iostream>
using namespace std;
int main()
{
SeqStack<int> sstack;
int ival=0;
while(cin>>ival)
sstack.Push(ival);
system("pause");
return 0;
}
#include"SeqStack.h"
#include<stdexcept>
#include<iostream>
using namespace std;
//pop,push
template<class T>
void SeqStack<T>::Push(const T& tobj)
{
if(IsFull())
OverFlow();
*top=tobj;
++top;
}
template<class T>
void SeqStack<T>::Pop()
{
if(IsEmpty())
throw runtime_error();
T* tmp=top;
--top;
delete tmp;
}
//isfull,empty
template<class T>
bool SeqStack<T>::IsFull()const
{
if(top==data+length)
return true;
}
template<class T>
bool SeqStack<T>::IsEmpty()const
{
if(top==data)
return true;
}
//size
template<class T>
unsigned int SeqStack<T>::Size()
{
return top-data+1;
}
//gettop
template<class T>
T& SeqStack<T>::GetTop()const
{
return *top;
}
//overflow
template<class T>
void SeqStack<T>::OverFlow()
{
throw runtime_error("stack is full");
}
//purge
template<class T>
void SeqStack<T>::Purge()
{
while(top!=data-1)
{
T* tmp=top;
--top;
delete tmp;
}
}
#ifndef SEQSTACK_H
#define SEQSTACK_H
#include<iostream>
template<class T>
class SeqStack
{
protected:
T* data;
T* top;
unsigned int length;
public:
SeqStack(unsigned int _length=5):data(new T[_length]),top(data),length(_length){}
~SeqStack(){delete [] data;}
void OverFlow();
void Push(const T&);
void Pop();
T& GetTop()const;
bool IsEmpty()const;
bool IsFull()const;
unsigned int Size();
void Purge();
};
#endif