So I understand there are tons of other questions just like this, but I've been searching for about 2h and haven't found one that has worked for me yet. In this code, I'm trying to create a simple stack using a class template; however, I keep getting the error: Invalid use of non-static data members when attempting to declare a pointer to my array (T*Arr). I get the same error when initializing my int ArrTop, but not my int ArrSize? Finally, I also get the errors cannot call member function without object when I attempt to call Top() and Empty() from within the .cpp file. Any help would be greatly appreciated!
Heres the code:
#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;
}
}
Then there are the errors I'm getting:
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
Again, thank you guys so much, you're the best!
Aucun commentaire:
Enregistrer un commentaire