这个抽象类型为什么不能实例化?
SeqStack.h#include <assert.h>
#include "iostream"
#include "Stack.h"
#include "iostream"
#include <cstdlib>
#include <stdio.h>
using namespace std;
const int stackIncreament = 20;
template <class T>
class SeqStack:public Stack<T>{
public:
SeqStack(int sz=50);
~SeqStack(){delete[]elements;}
void Push(const T& x);
bool Pop(T& x);
bool IsEmpty() const { return( top == -1);}
bool IsFull() const { return( top == maxSize-1);}
int getSize()const { return top+1;}
void overflowProcess();
void MakeEmpty() {top = -1;}
friend ostream& operator<< (ostream& os,SeqStack<T>& s){
private:
T *elements;
int top;
int x;
int maxSize;}
};
template<class T>
SeqStack<T>::SeqStack(int sz):top(-1), maxSize(sz)
{// Stack 类构造函数
elements = new T[ maxSize]
assert(elements!=NULL);
}
template<class T>
void SeqStack<T>::overflowProcess(){
T *newArray = new T[maxSize + stackIncreament];
if ( newArray = NULL){cerr<<"存储分配失败!"<<endl;exit(1);}
for (int i= 0; i<=top;i++) newArray[i] = elements[i];
maxSize = maxSize + stackIncreament;
delete[]elements;
elements = newArray;
};
template<class T>
void SeqStack<T>::Push(const T &x) {
if(IsFull()==true) overflowProcess();
elements[++top]=x;
};
template<class T>
bool SeqStack<T>::Pop(T &x) {
if(IsEmpty() == true) return false;
x = elements[top--];
return true;
};
template<class T>
ostream& operator<< (ostream& os, SeqStack<T>& s) {
os<<"top="<<s.top<<endl;
for(int i = 0;i <= s.top;i++)
os<<i<<":"<<s.elements[i]<<endl;
return os;
};
Stack.h
#ifndef Stack_
#define Stack_
#include "iostream"
using namespace std;
const int maxSize = 50;
template<class T>
class Stack
{
public:
Stack(){};
virtual bool IsEmpty() const = 0;
virtual int getSize() const = 0;
virtual bool IsFull() const = 0;
virtual bool getTop(T& x) const = 0;
virtual bool Pop(T& x) = 0;
virtual void Push(const T& x) = 0;
};
#endif
TestTime.cpp
#include "iostream"
#include <time.h>
#include "Stack.h"
#include "SeqStack.h"
typedef SeqStack<int> StackType;
using namespace std;
#pragma optimize("t", on)
int main()
{
SeqStack<int> s;
int n = 10000000; // number of push operations
int x = 2;
int temp=0;
long startTime = clock();
// push n elements
for (int i = 1; i <= n; i++)
s.Push(x);
// now pop them
for (int i = 1; i <= n; i++)
{
s.Pop(temp);
}
long elapsedTime = clock() - startTime;
cout << "Time for n = " << n << " stack operations is "
<< elapsedTime << " milliseconds" << endl;
return 0;
}
下面是在vs 2008 下的编译结果~ 用的Windows 7
>正在编译...
1>tt.cpp
1>g:\jimmy\documents\visual studio 2008\projects\tt\tt\tt.cpp(14) : error C2259: “SeqStack<T>”: 不能实例化抽象类
1> with
1> [
1> T=int
1> ]
1> 由于下列成员:
1> “bool Stack<T>::getTop(T &) const”: 是抽象的
1> with
1> [
1> T=int
1> ]
1> g:\jimmy\documents\visual studio 2008\projects\tt\tt\stack.h(21) : 参见“Stack<T>::getTop”的声明
1> with
1> [
1> T=int
1> ]
1>生成日志保存在“file://g:\jimmy\documents\Visual Studio 2008\Projects\tt\tt\Debug\BuildLog.htm”
1>tt - 1 个错误,0 个警告