<分区>
我现在一直在努力使用模板和类。 它假定我的构造函数的其他内容并需要一个类型说明符。 这是目前我程序中一大块问题的根源,如果该信息有用,我正在使用 Visual Studio 2017。
这是保存编码的 .cpp 文件
#include "stacks.h"
#include "main.h"
template <class A_Type> Stack<A_Type>::stacks()
{
top = -1;
};
template <class A_Type> A_Type Stack<A_Type>::push(A_Type x)
{
//Checking if stack is full before pushing.
if (top >= 8)
{
cout << "Stack Overflow \n";
}
//Stack is not full, then push int/float into stack.
else
{
a[++top] = x;
cout << "Element Inserted \n";
}
}
template <class A_Type> A_Type Stack<A_Type>::pop()
{
if (top < 0)
{
return 0;
}
else
{
A_Type d = a[top--];
return d;
}
}
template <class A_Type> A_Type Stack<A_Type>::peak()
{
A_Type p = a[top];
return p;
}
//wants to stay as template.
//Changed the A_Type behind Stack to an int.
template <class A_Type> int Stack<A_Type>::count()
{
int c = top + 1;
return c;
}
template <class A_Type> A_Type Stack<A_Type>::IsEmpty()
{
if (!count())
{
return true;
}
else
{
return false;
}
}
template class Stack<int>;
template class Stack<float>;
template class Stack<void>;
这是类所在的 .h 文件。
#ifndef CODING03_STACKS_H
#define CODING03_STACKS_H
template <class A_Type> class Stack
{
private:
int top; // count = top + 1
public:
//Constructor
stacks();
A_Type a[9]; // Stack's max size.
A_Type push(A_Type x);
A_Type pop();
A_Type peak();
int count();
A_Type IsEmpty();
};
#endif
我无法从类似的问题中找到答案。