![]() |
#2
寒风中的细雨2012-11-05 14:49
|

#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
#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?
#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?