新手写类模板的时候遇到的问题
//Stack.h
#ifndef STACK_H_
#define STACK_H_
const int MaxSize=50;
template <class T>
class Stack{
public:
Stack(){};
~Stack(){};
virtual void push(const T& x)=0;
virtual void pop()=0;
virtual T top()const=0;
virtual bool isEmpty()const=0;
virtual bool isFull()const=0;
};
#endif /*STACK_H_*/
//SeqStack.h
#ifndef SEQSTACK_H_
#define SEQSTACK_H_
#include"Stack.h"
template <class T>
class SeqStack:public Stack<T>{
public:
SeqStack(int MaxSize);
~SeqStack();
bool isEmpty()const;
void push(const T& x);
void pop();
T top()const{ return s[top];}
bool isFull()const;
void SetNull(){top=-1;}
private:
T *s;
int maxTop;
int top;//总是指向栈顶元素
};
#endif /*SEQSTACK_H_*/
//SeqStack.cpp
#include"SeqStack.h"
using namespace std;
template <class T>
SeqStack<T>::SeqStack(){
maxTop=MaxSize-1;
s=new T[MaxSize];
top=-1;
}
template <class T>
SeqStack<T>::~SeqStack(){
delete[] s;
}
template <class T>
void SeqStack<T>::push(const T& x){
s[++top]=x;
}
template <class T>
void SeqStack<T>::pop(){--top;}
//template <class T>
//T SeqStack<T>::top()const{return s[top];}
template <class T>
bool SeqStack<T>::isEmpty()const{
return top==-1;
}
template <class T>
bool SeqStack<T>::isFull()const{
return top==maxTop;
}
**** Build of configuration Debug for project C_unit3_stack ****
make -k all
make: Warning: File `objects.mk' has modification time 0.61 s in the future
Building file: ../SeqStack.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"SeqStack.d" -MT"SeqStack.d" -o"SeqStack.o" "../SeqStack.cpp"
In file included from ../SeqStack.cpp:1:
../SeqStack.h:18: error: declaration of `int SeqStack<T>::top'
../SeqStack.h:12: error: conflicts with previous declaration `T SeqStack<T>::top() const'
../SeqStack.cpp:4: error: prototype for `SeqStack<T>::SeqStack()' does not match any in class `SeqStack<T>'
../SeqStack.h:7: error: candidate is: SeqStack<T>::SeqStack(int)
../SeqStack.cpp:4: error: template definition of non-template `SeqStack<T>::SeqStack()'
make: *** [SeqStack.o] Error 1
make: Target `all' not remade because of errors.
make: warning: Clock skew detected. Your build may be incomplete.
Build complete for project C_unit3_stack