【发布时间】:2020-03-02 18:45:29
【问题描述】:
所以我知道还有很多其他类似的问题,但我一直在寻找大约 2 小时,但还没有找到对我有用的问题。在这段代码中,我尝试使用类模板创建一个简单的堆栈;但是,我不断收到错误消息:尝试声明指向我的数组的指针 (T*Arr) 时,非静态数据成员的使用无效。初始化我的 int ArrTop 时出现同样的错误,但不是我的 int ArrSize?最后,当我尝试从 .cpp 文件中调用 Top() 和 Empty() 时,我还收到错误无法调用没有对象的成员函数。任何帮助将不胜感激!
代码如下:
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
const int DEFAULTSIZE=100;
template <class T>
class Stack {
public:
Stack(); // Default Constructor, stack is size 100.
Stack(const int size); // Constructor, creates stack of size "size"
Stack(const Stack<T> & item);// Copy constructor
bool Full(); // Return true if the stack is full
bool Empty(); // Return true if the stack is empty
int Size(); // Return the size of the stack
T Top(); // Returns the top element, does not pop it.
bool Push (const T item); // Put an item on the stack.
bool Pop(); // Pop an item off and display to std out
friend ostream &operator <<(ostream & os, Stack<T> &s)
{
if(Empty())
{
cout<<"Stack underflow!";
}
else
{
cout<<"Element: "<<Top()<<"has been removed"<<endl;
Arr[ArrTop--];
}
}
private:
T *Arr; // The "stack"
int ArrSize; // The number of elements the stack can hold
int ArrTop; // Points to the first empty node
};
template <class T>
Stack<T>::Stack()
{
Arr = new T[DEFAULTSIZE];
ArrSize = DEFAULTSIZE;
ArrTop = -1;
}
//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//
//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
template <class T>
Stack<T>::Stack(const int size)
{
Arr = new T[size];
ArrSize=size;
ArrTop = -1;
}
//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//
//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
template <class T>
bool Stack<T>::Empty()
{
if(ArrTop==-1)
return true;
}
//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//
//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
template <class T>
int Stack<T>::Size()
{
return ArrTop+1;
}
//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//
//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
template <class T>
bool Stack<T>::Full()
{
if(Size()==ArrSize)
return true;
}
template <class T>
bool Stack<T>::Pop()
{
if(Empty())
{
cout<<"Stack underflow!";
return false;
}
cout<<"Element: "<<Top()<<"has been removed"<<endl;
Arr[ArrTop--];
return true;
}
//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//
//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
template <class T>
T Stack<T>::Top()
{
if(!Empty())
{
return Arr[ArrTop];
}
else
{
exit(EXIT_FAILURE);
}
}
//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//
//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
template <class T>
bool Stack<T>::Push(const T item)
{
if(Full())
{
cout<<"Stack overflow!";
return false;
}
else
{
cout<<"Element: "<<item<<"has been inserted!"<<endl;
Arr[ArrTop++]=item;
}
}
然后是我得到的错误:
In file included from main.cpp:2:0:
stack.cpp: In function ‘std::ostream& operator<<(std::ostream&, Stack<T>&)’:
stack.cpp:45:9: error: invalid use of non-static data member ‘Stack<T>::Arr’
T *Arr; // The "stack"
^
stack.cpp:40:3: error: from this location
Arr[ArrTop--];
^
stack.cpp:47:10: error: invalid use of non-static data member ‘Stack<T>::ArrTop’
int ArrTop; // Points to the first empty node
^
stack.cpp:40:7: error: from this location
Arr[ArrTop--];
^
stack.cpp: In instantiation of ‘std::ostream& operator<<(std::ostream&, Stack<char>&)’:
main.cpp:29:16: required from here
stack.cpp:33:11: error: cannot call member function ‘bool Stack<T>::Empty() [with T = char]’ without object
if(Empty())
^
stack.cpp:39:26: error: cannot call member function ‘T Stack<T>::Top() [with T = char]’ without object
cout<<"Element: "<<Top()<<"has been removed"<<endl;
^
make: *** [main.o] Error 1
再次感谢你们,你们是最棒的!
【问题讨论】:
-
提示:std::stack 已经存在。无需自己动手。
-
我知道这是作业!
-
不是问题,但输出运算符应该采用
const参考 -
您的
operator<<函数在语义上是错误的。它是流输出运算符,它应该将堆栈的内容打印到流os。而且它不是“pop”运算符。 -
您是否真的想实现一个
Stack::operator<<来从堆栈中弹出一个元素?