C++问题:'print' : is not a member of 'Stack<T>'怎么解决
#include<iostream>usingnamespacestd;template<typenameT>classpushOnFull{T_value;public...
#include <iostream>
using namespace std;
template < typename T > class pushOnFull
{
T _value;
public:
pushOnFull( T i )
{
_value = i;
}
T value()
{
return _value;
}
void print()
{
cout<<"Stack is fall,"<<_value<<"is not pushed"<<endl;
}
};
template < typename T> class popOnEmpty
{
public:
void print()
{
cout<<"stack is empty ,con't pop"<<endl;
}
};
template < typename T > class Stack
{
int top;
T *elements;
int maxSize;
public:
Stack( int =20);
~Stack()
{
delete[] elements;
}
void Push(const T & data);
T Pop();
T GetEle(int i)
{
return element[i];
}
void makeEmpty()
{
Top=-1;
}
bool IsEmpty()const
{
return top==-1;
}
bool IsFull()const
{
return top==maxSize-1;
}
void printStack();
};
template <typename T>Stack<T>::Stack(int maxs)
{
maxSize=maxs;
top=-1;
elements= new T[maxSize];
}
template <typename T>void Stack<T>::printStack()
{
for(int i=0; i<top ; i++)
cout<<elements[i]<<"";
cout<<endl;
}
template <typename T>void Stack<T>::Push( const T&data)
{
if(IsFull())
throw pushOnFull<T>(data);
elements[++top]=data;
}
template <typename T> T Stack<T>::print()
{
if(IsEmpty())
throw popOnEmpty<T>();
return elements[top--];
}
int main()
{
int a[9]={1,8,7,6,5,4,3,2,1} , b[9]={0},i ;
Stack <int> istack(8);
try
{
for(int i=0; i<9 ; i++)
istack.Push(a[i]);
istack.printStack();
}
catch(pushOnFull<int> & eobj)
{
eobj.print();
}
try
{
for(int i=0; i<9 ; i++)
b[i]=istack.Pop(a[i]);
istack.printStack();
}
catch(popOnEmpty<int> & eobj)
{
eobj.print();
}
cout<<"Pop order is:";
for(int i=0;i<9;i++)
cout<<b[i]<<"";
cout<<endl;
return 0;
}
错误信息:
D:\软件\VC6\MyProjects\sss\sss.cpp(77) : error C2039: 'print' : is not a member of 'Stack<T>'
D:\软件\VC6\MyProjects\sss\sss.cpp(100) : error C2660: 'Pop' : function does not take 1 parameters
D:\软件\VC6\MyProjects\sss\sss.cpp(108) : error C2086: 'i' : redefinition
执行 cl.exe 时出错. 展开
using namespace std;
template < typename T > class pushOnFull
{
T _value;
public:
pushOnFull( T i )
{
_value = i;
}
T value()
{
return _value;
}
void print()
{
cout<<"Stack is fall,"<<_value<<"is not pushed"<<endl;
}
};
template < typename T> class popOnEmpty
{
public:
void print()
{
cout<<"stack is empty ,con't pop"<<endl;
}
};
template < typename T > class Stack
{
int top;
T *elements;
int maxSize;
public:
Stack( int =20);
~Stack()
{
delete[] elements;
}
void Push(const T & data);
T Pop();
T GetEle(int i)
{
return element[i];
}
void makeEmpty()
{
Top=-1;
}
bool IsEmpty()const
{
return top==-1;
}
bool IsFull()const
{
return top==maxSize-1;
}
void printStack();
};
template <typename T>Stack<T>::Stack(int maxs)
{
maxSize=maxs;
top=-1;
elements= new T[maxSize];
}
template <typename T>void Stack<T>::printStack()
{
for(int i=0; i<top ; i++)
cout<<elements[i]<<"";
cout<<endl;
}
template <typename T>void Stack<T>::Push( const T&data)
{
if(IsFull())
throw pushOnFull<T>(data);
elements[++top]=data;
}
template <typename T> T Stack<T>::print()
{
if(IsEmpty())
throw popOnEmpty<T>();
return elements[top--];
}
int main()
{
int a[9]={1,8,7,6,5,4,3,2,1} , b[9]={0},i ;
Stack <int> istack(8);
try
{
for(int i=0; i<9 ; i++)
istack.Push(a[i]);
istack.printStack();
}
catch(pushOnFull<int> & eobj)
{
eobj.print();
}
try
{
for(int i=0; i<9 ; i++)
b[i]=istack.Pop(a[i]);
istack.printStack();
}
catch(popOnEmpty<int> & eobj)
{
eobj.print();
}
cout<<"Pop order is:";
for(int i=0;i<9;i++)
cout<<b[i]<<"";
cout<<endl;
return 0;
}
错误信息:
D:\软件\VC6\MyProjects\sss\sss.cpp(77) : error C2039: 'print' : is not a member of 'Stack<T>'
D:\软件\VC6\MyProjects\sss\sss.cpp(100) : error C2660: 'Pop' : function does not take 1 parameters
D:\软件\VC6\MyProjects\sss\sss.cpp(108) : error C2086: 'i' : redefinition
执行 cl.exe 时出错. 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询