samedi 14 mars 2015

Request for member which is not is of non class


I have scoured this site and others and couldn't find any help with this. I have no idea where I am going wrong.


I am trying to test if a stack is empty (or full in some cases). For some reason, I keep getting this error. I played around with code but it didn't help, it only gave rise to more bugs. Please take a look at the code and tell me where I have gone wrong. Thanks!



#include<iostream>
#include <cstdlib>
#include <string>
#include <stdexcept>

using namespace std;

template <class TYPE> class Stack {
template<class T> //<----note this: i.e make it template!
friend ostream& operator << (ostream &, const Stack<TYPE> &);

private:
int size; //size of stack
int stackMax; //stack capacity
TYPE *aStack; //an array for entries in the stack

public:
Stack(){

size = 0;
stackMax = 32;
aStack = new TYPE [stackMax];

}//constructor

~Stack(){
}//destructor

//This function tests if a stack is empty.
bool empty(){

if(size == 0){
return true;
}

return false;

}//end empty function

//This function tests if a stack is full

bool full(){

if(size == stackMax){
return true;
}

return false;

}//end full function

//This function removes the top entry off aStack.
void pop(){

if (aStack.empty()){
cout <<"Error: stack empty\n";
}else{
size--;
}

}//end pop function

//This function pushes an entry onto aStack.
void push(TYPE entry) {

if(!aStack.full()){
aStack[size++] = entry;
}else{
cout<< "Error: stack full\n";
}

}//end push function

//this function returns the current top entry
TYPE top(){
if(aStack.empty()){

cout <<"Error: stack empty\n";

}
return aStack[size-1];

}//end top function


};//end Stack class

template<class TYPE> //<----note this: i.e make it template!
ostream& operator<<(ostream &out, const Stack<TYPE> &aStack){
int size = aStack.size();
out << "stack: ";
if(size == 0)
out << "empty";
else{
out << aStack[size];
for (size ; size = 0 ; size--){
out << "\n" << aStack[size];
}
}
return out;
}//end ostream function

int main() {
Stack<int> intStack; // stack of ints
Stack<string> stringStack; // stack of strings

// manipulate int stack
intStack.push(2);
intStack.push(3);
intStack.push(4);
intStack.push(5);
intStack.push(6);
intStack.push(7);
intStack.top();
intStack.pop();
cout << intStack;

// manipulate string stack
stringStack.push ("hello");
stringStack.pop();


}




Aucun commentaire:

Enregistrer un commentaire