| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 372 人关注过本帖
标题:链式栈的建立,调试时发生一个错误,求高手看看是怎么错了?
只看楼主 加入收藏
回到原點
Rank: 1
等 级:新手上路
帖 子:7
专家分:3
注 册:2012-10-29
结帖率:33.33%
收藏
已结贴  问题点数:10 回复次数:2 
链式栈的建立,调试时发生一个错误,求高手看看是怎么错了?
程序代码:
#ifndef _LINK_H
#define _LINK_H
// A singly-linked list node
template<class Elem> class Link { 

public:
  Elem element; // Elem value for this node
  Link *next;  // Pointer to next node in list
  Link(const Elem elemval, Link* nextval =NULL)

  { element = elemval;  next = nextval; }    

  Link(Link* nextval =NULL) { next = nextval; }

};
#endif#ifndef _STACK_H
#define _STACK_H
template <class Elem> class Stack {
public:
  virtual void clear() = 0;
  virtual bool push(const Elem&) = 0;
  virtual bool pop(Elem&) = 0;
  virtual bool topValue(Elem&) const = 0;
  virtual int length() const = 0;
};
#endif
程序代码:
#ifndef _LSTACK_H
#define _LSTACK_H
#include "Stack.h"
#include "Link.h"
//link list-based stack implementation
template<class Elem> class LStack: public Stack<Elem>{
private:
    Link<Elem>* top; // Pointer to first elem
    int size;        // Count number of elems
public:
    LStack(int sz = DefaultListSize){top = NULL; size = 0;}
    ~LStack() {clear();}
    void clear()

    {
        while(top != NULL)
        {   //delete link node
            Link<Elem> *temp = top;
            top = top->next;
            size = 0;
            delete temp;
        }
    }
    bool push(const Elem& item)
    {
        top = new Link<Elem>(item, top);
        size++;
        return true;
    }
bool pop(Elem& it)
{
        if(size == 0) return false;
        it = top->element;
        Link<Elem>* ltemp = top->next;
        delete top;
        top = ltemp;
        size--;
        return true;
}

    bool topValue(Elem& it) const
    {
        if(size == 0) return false;
        it = top->element;
        return true;
    }

    int legth() const{return size;}
};
#endif#include "stdafx.h"
#include "LStack.h"
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
    printf("Hello World!\n");
    int i,num,it,its,topval;

    cout<<"请输入您要输入数据的个数:"<<endl;
    cin>>num;
    LStack<int>  lstackone();
    cout<<"请输入"<<num<<"个数据(数据入栈):"<<endl;
    for(i=0;i<num;i++)
    {
        cin>>it;
        lstackone.push(it);
    }
    cout<<"输出栈顶的数据:"<<endl;
    lstackone.topValue(topval);
    cout<<topval<<endl;
    cout<<"栈的长度:"<<endl;
    cout<<lstackone.length()<<endl;

    cout<<"数据出栈,显示如下:"<<endl;
    for(i=0;i<num;i++)
    {
        lstackone.pop(its);
        cout<<its<<endl;
    }

    return 0;
}
如下是错误信息:
F:\工作区间\LStack\LStack.cpp(19) : error C2228: left of '.push' must have class/struct/union type
F:\工作区间\LStack\LStack.cpp(22) : error C2228: left of '.topValue' must have class/struct/union type
F:\工作区间\LStack\LStack.cpp(25) : error C2228: left of '.length' must have class/struct/union type
F:\工作区间\LStack\LStack.cpp(29) : error C2228: left of '.pop' must have class/struct/union type
执行 cl.exe 时出错.

LStack.exe - 1 error(s), 0 warning(s)
请问那里出错了?不是已经建立了栈吗?怎么会提示left of '.push' must have class/struct/union type?
搜索更多相关主题的帖子: element 
2012-11-04 22:36
寒风中的细雨
Rank: 17Rank: 17Rank: 17Rank: 17Rank: 17
等 级:贵宾
威 望:66
帖 子:1710
专家分:8645
注 册:2009-9-15
收藏
得分:10 
    int legth() const{return size;}
错误是    抽象类  不能实例化


2012-11-05 14:49
寒风中的细雨
Rank: 17Rank: 17Rank: 17Rank: 17Rank: 17
等 级:贵宾
威 望:66
帖 子:1710
专家分:8645
注 册:2009-9-15
收藏
得分:0 
template<class Elem>
class LStack: public Stack<Elem>
{
};
继承了抽象类    没有全部实现抽象类中的纯虚函数         


要保持  函数的返回值 和签名一致  
2012-11-05 14:52
快速回复:链式栈的建立,调试时发生一个错误,求高手看看是怎么错了?
数据加载中...
 
   



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

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