Well, I had an assignment for class to convert an array of objects stack to an array of pointers stack. I think that I've oversimplified the assignment, because the program compiles okay, but whenever I push an item onto the stack, the program crashes. Using debug, it looks like it was assigning a value of 5 to my pointer, which is the max # of items in my array. I can't figure out why this is. Also, this is a class, which I'm not exactly comfortable with yet.

Scroll to the bottom if you want to see the actual problem without the long explanation. Sorry for the backwards post.

This is my class:

Code:
#ifndef STACKTYPE_H
#define STACKTYPE_H
#include "ItemType.h"

//   The user of this file must provide a file "ItemType.h" that defines:
//       ItemType : the class definition of the objects on the stack.
//       MAX_ITEMS: the maximum number of items on the stack. 

//   Class specification for Stack ADT in file Stack1.h


class FullStack
// Exception class thrown by Push when stack is full.
{};

class EmptyStack
// Exception class thrown by Pop and Top when stack is emtpy.
{};


class StackType
{
public:
  StackType(int);                 // added 10/9/2003
  StackType();
  // Class constructor.
  bool IsFull() const;
  // Function: Determines whether the stack is full.
  // Pre:  Stack has been initialized.
  // Post: Function value = (stack is full)
  bool IsEmpty() const;
  // Function: Determines whether the stack is empty.
  // Pre:  Stack has been initialized.
  // Post: Function value = (stack is empty)
  void Push( const ItemType & item);
  // Function: Adds newItem to the top of the stack.
  // Pre:  Stack has been initialized.
  // Post: If (stack is full), FullStack exception is thrown;
  //     otherwise, newItem is at the top of the stack.
  void Pop();
  // Function: Removes top item from the stack.
  // Pre:  Stack has been initialized.
  // Post: If (stack is empty), EmptyStack exception is thrown;
  //     otherwise, top element has been removed from stack.
  ItemType Top();
  // Function: Returns a copy of top item on the stack.
  // Pre:  Stack has been initialized.
  // Post: If (stack is empty), EmptyStack exception is thrown;
  //     otherwise, top element has been removed from stack.

       
private:
  int top;
  ItemType ** items;		//  changed 10/9/2003
};
#endif
That part is fine I think.

This is ItemType if you're curious:

Code:
// ItemType.h StackDriver
#ifndef ITEMTYPE_H
#define ITEMTYPE_H
const int MAX_ITEMS = 5;
typedef int ItemType;

#endif
This is my driver program:

Code:
// Test driver
#include <iostream>
#include <fstream>

#include "StackType.h"

using namespace std;
bool copyStack( StackType & d, StackType & s)
{
	StackType temp;
	while ( !s.IsEmpty() )
	{
	     temp.Push(	   s.Top()  );
		 s.Pop();
	}
	return true;
}

int main()
{
  StackType s;
  ItemType work;
  while ( ! s.IsFull())
  { 
	  cin >> work;
	  s.Push(work);
  }
  //  empty the stack - printing the data
  while ( !s.IsEmpty() )
  {
	  work = s.Top();
	  cout << work;
	  s.Pop();
  }



  return 0;
}
I believe that is fine as well. I don't think it should have to change?

Finally, this is my stack:

Code:
/ File: StackType.cpp

#include "StackType.h"
#include <iostream>
ItemType dummy;

StackType::StackType()
{
  top = -1;
}

bool StackType::IsEmpty() const
{
  return (top == -1);
}

bool StackType::IsFull() const
{
  return (top ==  MAX_ITEMS-1);
}

void StackType::Push(const ItemType & newItem)
{
  if( IsFull() )
    throw FullStack();
  top++;
  *items[top] = newItem;
}

void StackType::Pop()
{
  if( IsEmpty() )
    throw EmptyStack();
  *items[top] = dummy;
  top--;
}

ItemType StackType::Top()
{
  if (IsEmpty())
    throw EmptyStack();
  return *items[top];
}
I'm quite sure this is where my problem lies. I think that I can't just do this:

*items[top] = newItem;

as that seems to be where the problem occurs. How would you go about pushing the value onto the stack? I don't really understand how pointers change this.

Also, sorry for posting so much code. But last time I didn't post enough, so I thought I would avoid that problem this time.