Hi.
I'm trying to implement a stack in C++, but i got stuck

Here's my code

#include<iostream>
#include<cassert>
#include<cstdlib>

using namespace std;

template<class Type>
class stackTDA
{
public:
  virtual void initializeStack() = 0;
  virtual bool isEmptyStack() = 0;
  virtual bool isFullStack() = 0;
  virtual void push(const Type& newItem) = 0;
  virtual Type top() const = 0;
  virtual void pop() = 0;
};


// ... stack ...
template<class Type>
class stack : public stackTDA<Type>
{
public:
  const stack<Type>& operator = (const stack<Type>&);

  void initializeStack();

  bool isEmptyStack() const;

  bool isFullStack() const;

  void push(const Type& newItem);

  Type top() const;

  void pop();

  stack(int intstackSize = 100);

  stack(const stack<Type>& otherStack);

  ~stack();

private:
  int intmaxStackSize;
  int intstackTop;
  Type *list;
};

// inicializa la pila
template<class Type>
void stack<Type>::initializeStack()
{
  intstackTop = 0;
}

// pila vacía
template<class Type>
bool stack<Type>::isEmptyStack() const
{
  return (intstackTop == 0);
}

// pila llena
template<class Type>
bool stack<Type>::isFullStack() const
{
  return (intstackTop == intmaxStackSize);
}

// push
template<class Type>
void stack<Type>::push(const Type& newItem)
{
  if (!isFullStack())
  {
    list[intstackTop] = newItem;
    intstackTop++;
  }
  else
    cout << "No se puede agregar,  pila llena" << endl;
}

// regresa el elemento más arriba de la pila
template<class Type>
Type stack<Type>::top() const
{
  assert(intstackTop != 0);
  return list[intstackTop - 1];
}

// pop
template<class Type>
void stack<Type>::pop()
{
  if (!isEmptyStack())
    intstackTop--;
  else
    cout << "No se puede quitar elementos a una lista vacía" << endl;
}


// constructor & destructor
template<class Type>
stack<Type>::stack(int intstackSize)
{
  if (intstackSize <= 0)
  {
    cout << "el tamaño de la lista debe de ser positivo" << endl;
    cout << "creando un arreglo de tamaño 100" << endl;

    intmaxStackSize = 100;
  }
  else
    intmaxStackSize = intstackSize;

  intstackTop = 0;
  list = new Type[intmaxStackSize];
}

//  destructor
template<class Type>
stack<Type>::~stack()
{
  delete [] list;
}

// ++++++++++++++++++++++++++++++++++++++
// ++++++++++++++++++++++++++++++++++++++
int main()
{
  int intLim = 100;
  stack<int> stack1;

  for (intLim = 0;intLim<100;intLim++)
  {
    int b = rand() % 100 + 1;
    stack1.push(b);
  }

  cout << "Los elementos de la pila: " << endl;
  while (!stack1.isEmptyStack())
  {
    cout << stack1.top() << " ";
    stack1.pop();
  }
}

When i try to compile it, this error appears:
error: cannot declare variable ‘stack1’ to be of abstract type ‘stack<int>’ stack<int> stack1

Any idea on how to fix this? (o.O) (?)

You have mismatching const on your functions. In the derived class, you have:

  bool isEmptyStack() const;
  bool isFullStack() const;

And in the base class you have:

  virtual bool isEmptyStack() = 0;
  virtual bool isFullStack() = 0;

The base class functions should be:

  virtual bool isEmptyStack() const = 0;
  virtual bool isFullStack() const = 0;

The compiler says that the stack class is abstract because your derived-class functions did not match the base-class ones, and therefore, left pure virtual functions remaining without a derived-class implementation.

Wow, thanks Mike, this worked like a charm :)

I took this example from a book.

And again, thanks a lot.

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.