简体   繁体   English

模板类错误C ++

[英]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(24):错误C2955:'SimpleStack':使用类模板需要模板参数列表

simplestack.h(9) : see declaration of 'SimpleStack' simplestack.h(9):看'SimpleStack'的声明

simplestack.h(28): error C2244: 'SimpleStack::push' : unable to match function definition to an existing declaration simplestack.h(28):错误C2244:'SimpleStack :: push':无法将函数定义与现有声明匹配

simplestack.h(12) : see declaration of 'SimpleStack::push' simplestack.h(12):看'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. 注意:每次我尝试将MAX_SIZE设为T时都不会接受它。 Thank you for any help. 感谢您的任何帮助。

方法push应返回SimpleStack<T>& ,而不是SimpleStack&

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM