This is from my .hpp file.
struct Item{
std::string todo;};
const int MAX_STACK_SIZE = 5;
class StackArray
{
public:
StackArray();
bool isEmpty();
bool isFull();
void push(std::string Item);
void pop();
Item* peek();
int getStackTop() { return stackTop; }
Item** getStack() { return stack; }
private:
int stackTop;
Item* stack[MAX_STACK_SIZE];
};
#endif
And the following is part function from my .cpp file.
void StackArray::push(std::string Item)
{
if (isFull())
{
cout<<"Stack full, cannot add new todo item."<<endl;
}
else
{
stackTop++;
Item* newStack = new Item[MAX_STACK_SIZE];
newStack[stackTop].todo = Item;
}
}
I really confused about printing out the stack array in main.cpp file. How can I do that? Here is now I got, but can only print out the address.
int main()
{
StackArray stackArray;
if (stackArray.isEmpty())
cout<< "Empty stack." <<endl;
stackArray.push("25");
stackArray.push("20");
stackArray.push("15");
stackArray.push("10");
Item**stack1=new Item*[5];
*stack1=new Item;
stack1=stackArray.getStack();
for(int i=0;i<5;i++)
{
cout<<*(stack1+i)<<endl;
}
}
newStackanywhere permanent, so you're leaking memory.stack?cout<<stack1[i]->todo;Except that it'll probably crash, because you never in fact initialize the contents ofStackArray::stack; it contains random garbage.