#include "stdafx.h"
#include
#include
using namespace std;
templateclass stack {
int top;
T *elements;
int maxsize;
public:
stack(int = 20);
~stack() { delete[]elements; }
void push(const T&x);
T pop();
T getelem(int i);
void makeempty() { top = -1; }
bool isempty()const { return top == -1; }
bool isfull()const { return top == maxsize - 1; }
friend ostream &operator<<(ostream&, const stack&);//T必不可少
};
templatestack::stack(int max) {
maxsize = max;
top = -1;
elements = new T[maxsize];
assert(elements != 0);
}
template ostream& operator<<(ostream& s, const stack& s1) {//T必不可少
for (int i = 0; i <= s1.top; i++) s << s1.elements[i] << ' ';
s << endl;
return s;
}
templatevoid stack::push(const T&data) {
assert(!isfull());
elements[++top] = data;
}
templateT stack::pop() {
assert(!isempty());
return elements[top--];
}
templateT stack::getelem(int i) {
assert(i <= top&&i >= 0);
return elements[i];
}
void main()
{
stack s;
s.push(1);
cout << s << endl;
}
c++运算符重载vs2015中报错,而用vc6.0不会报错
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-