Kayla Bianchi
Kayla Bianchi

Reputation: 67

Template Class error C++

I am learning how to make a template class and I follow the concept but am running into an error. I have a class that I turned into a template but I get the following errors;

simplestack.h(24): error C2955: 'SimpleStack' : use of class template requires template argument list

simplestack.h(9) : see declaration of 'SimpleStack'

simplestack.h(28): error C2244: 'SimpleStack::push' : unable to match function definition to an existing declaration

simplestack.h(12) : see declaration of 'SimpleStack::push'

This is my code:

const int MAX_SIZE = 100; 
template <typename T>
class SimpleStack
{
public:
  SimpleStack();
  SimpleStack & push(T value);
  T pop();

private:
  T items[MAX_SIZE];
  T top;
};
template <typename T>
SimpleStack<T>::SimpleStack() : top(-1)
{}

template <typename T>
SimpleStack &SimpleStack<T>::push(T value)
{
  items[++top] = value;
  return *this;
}

template <typename T>
T SimpleStack<T>::pop()
{
  return items[top--];
}

Note: every time I try to chance MAX_SIZE to T it won't accept it. Thank you for any help.

Upvotes: 1

Views: 160

Answers (1)

Dabbler
Dabbler

Reputation: 9863

The method push should return SimpleStack<T>&, not SimpleStack&.

Upvotes: 4

Related Questions