| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2348 人关注过本帖
标题:这个抽象类型为什么不能实例化?
只看楼主 加入收藏
isbn0000
Rank: 1
等 级:新手上路
帖 子:21
专家分:0
注 册:2009-3-21
结帖率:75%
收藏
 问题点数:0 回复次数:3 
这个抽象类型为什么不能实例化?
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 个警告
搜索更多相关主题的帖子: 类型 实例 
2009-11-18 03:45
isbn0000
Rank: 1
等 级:新手上路
帖 子:21
专家分:0
注 册:2009-3-21
收藏
得分:0 
上面这个问题已经解决了~  但有了新的问题~
1>------ 已启动生成: 项目: tt, 配置: Debug Win32 ------
1>正在编译...
1>tt.cpp
1>g:\jimmy\documents\visual studio 2008\projects\tt\tt\seqstack.h(33) : error C2062: 意外的类型“void”
1>        g:\jimmy\documents\visual studio 2008\projects\tt\tt\seqstack.h(30): 编译类 模板 成员函数“SeqStack<T>::SeqStack(int)”时
1>        with
1>        [
1>            T=int
1>        ]
1>        g:\jimmy\documents\visual studio 2008\projects\tt\tt\tt.cpp(15): 参见对正在编译的类 模板 实例化“SeqStack<T>”的引用
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 个警告
========== 生成: 成功 0 个,失败 1 个,最新 0 个,跳过 0 个 ==========
2009-11-18 11:09
isbn0000
Rank: 1
等 级:新手上路
帖 子:21
专家分:0
注 册:2009-3-21
收藏
得分:0 
在删除顺序栈的构造函数那段代码后~
1>------ 已启动生成: 项目: tt, 配置: Debug Win32 ------
1>正在编译...
1>tt.cpp
1>正在编译资源清单...
1>Microsoft (R) Windows (R) Resource Compiler Version 6.0.5724.0
1>Copyright (C) Microsoft Corporation.  All rights reserved.
1>正在链接...
1>tt.obj : error LNK2019: 无法解析的外部符号 "public: __thiscall SeqStack<int>::SeqStack<int>(int)" (??0?$SeqStack@H@@QAE@H@Z),该符号在函数 _main 中被引用
1>tt.obj : error LNK2001: 无法解析的外部符号 "public: virtual bool __thiscall Stack<int>::getTop(int &)" (?getTop@?$Stack@H@@UAE_NAAH@Z)
1>G:\jimmy\documents\Visual Studio 2008\Projects\tt\Debug\tt.exe : fatal error LNK1120: 2 个无法解析的外部命令
1>生成日志保存在“file://g:\jimmy\documents\Visual Studio 2008\Projects\tt\tt\Debug\BuildLog.htm”
1>tt - 3 个错误,0 个警告
========== 生成: 成功 0 个,失败 1 个,最新 0 个,跳过 0 个 ==========
2009-11-18 11:11
liuxingbei
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2015-7-31
收藏
得分:0 
楼主是怎么解决的,我也出现这样的问题了
2015-07-31 16:28
快速回复:这个抽象类型为什么不能实例化?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015495 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved