1

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.

1 Answer 1

4

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

Sign up to request clarification or add additional context in comments.

1 Comment

In the definition of the member function. In the declaration it doesn't matter due to injected class name.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.