#include <iostream>
#include "IntStack.h"

int main()
{
        int i;
	TIntStack a(5);
	TIntStack b(20);
	TIntStack *ip = new TIntStack;

	for (i=1; i <=5; i++)  {
		a.Push(i);
		b.Push(-i);
	}

	for (; i <=10; i++)
		b.Push(-i);

	TIntStack::PrintStack(a);
	a = b;
	TIntStack::PrintStack(*ip);
	delete ip;

	return 0;
}
#ifndef INTSTACK_H
#define INTSTACK_H

const unsigned int DEFAULT_SIZE = 256;

class TIntStack  {
	public :
		TIntStack(unsigned int stackSize = DEFAULT_SIZE);	//Default constructor
		TIntStack(const TIntStack& that); 							//Copy constructor
		TIntStack& operator=(const TIntStack& assign);		//Assignment operator
		~TIntStack();

		void Push(int thisValue);
		int Pop();
		unsigned int HowMany() const;
        void PrintStack(TIntStack);
	private :
		int* _sp;
		unsigned _count;
		unsigned _size;
};

#endif
#include "IntStack.h"

TIntStack::TIntStack(unsigned int stackSize /*  = DEFAULT_SIZE  */)
{
	//Allocate memory only when stackSize is positive
	If (stackSize > 0)  {
		_size = stackSize;
		_sp = new int[_size];			//Allocate memory for stack elements
		for (int i=0; i < _size; i++)		//Initialize all elements to zero
			_sp[i] = 0;
	}
	else  {
		_sp = 0;								//Set the pointer to the unique value
		_size = 0;
	}
	_count = 0;								//No elements on the stack
}

TIntStack::TIntStack(const TIntStack& source)
{
	_size = source._size;
	if (_size > 0)  {
		_sp = new int [_size];
		_count = source._count;
		for (int i=0; i < _count; i++)
			_sp[i] = source._sp[i];
	}
	else  {
		_sp = 0;
		_count = 0;
	}
}

TIntStack& TIntStack::operator=(const TIntStack& source)
{
	if (this == &source)  {
		cout << "Warning: Assignment to self.\n";
		return *this;
	}

	if (source._count > this->_size)  {
		delete [] _sp;
		this->_size = source._size;
		_sp = new int [ this->_size ];
	}

	for (int i = 0; i < source.count; i++)
		this->_sp[i] = source._sp[i];
	this->_count = source->count;

	return *this;
}

TIntStack::~TIntStack()
{
	cout << "Executing the destructor for TIntStack\n";
	delete [] _sp;
}

TIntStack::Push(int what)
{
	if (_count < _size)  {
		_sp[_count] = what;
		_count++;
	}
	else  {
		cout << "Stack is FULL. Cannot push value" << what << endl;
	}
}

int TIntStack::Pop()
{
	if (_count <= 0 )  {
		cout << " Stack is EMPTY\n";
		exit (1);
	}
	_count--;
	return _sp[_count];
}

unsigned TIntStack::HowMany() const
{
	return _count;
}

void TIntStack::PrintStack(TIntStack thisOne)
{
	unsigned i = thisOne.HowMany();
	for (unsigned j = 0;  j < i; j++)
		cout << "[" << thisOne.Pop() << "]" <<endl;
}

For both TIntStack::PrintStack function i got the same error saying that <cannot call member function 'void TIntStack::PrintStack(TIntStack)' without object>. What is that suppose to mean?

Unless a member function is declared as static, you need an object instance to call it on:

a.PrintStack(a);

You can either make it static, or change the member function to work on this rather than a parameter:

void TIntStack::PrintStack()
{
	unsigned i = HowMany();
	for (unsigned j = 0;  j < i; j++)
		cout << "[" << Pop() << "]" <<endl;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.