数据结构关于顺序表的进栈的问题
时间:2011-10-29
来源:互联网
我在做数据结构的练习,把顺序栈的类定义和实现都写了,测试的时候发现进栈出了问题,也就是 stack.push(x); 这句
编译器报错如下:
c:\documents and settings\longkai\my documents\visual studio 2010\projects\stack\stack\seqstack.h(45): 编译类 模板 成员函数“void SeqStack<T>::overflowProcess(void)”时
with
[
T=int
]
c:\documents and settings\longkai\my documents\visual studio 2010\projects\stack\stack\seqstack.h(39): 编译类 模板 成员函数“SeqStack<T>::SeqStack(int)”时
with
[
T=int
]
c:\documents and settings\longkai\my documents\visual studio 2010\projects\stack\stack\main.cpp(9): 参见对正在编译的类 模板 实例化“SeqStack<T>”的引用
with
[
T=int
]
一下是具体的程序,请大家帮帮忙!
#include <assert.h>
#include <iostream>
const int stackIncreament = 20;
template <class T>
class SeqStack {
private:
T* elements;
int top;
int maxSize;
void overflowProcess();
public:
SeqStack(int sz = 50);
~SeqStack() { delete[] elements; }
void push(T& x);
bool pop(T& x);
bool getTop(T& x);
bool isEmpty() { return top == -1 ? true : false; }
bool isFull() { return top == maxSize - 1 ? true : false; }
int getSize() { return top + 1; }
void makeEmpty() { top = -1; }
//friend ostream& operator << (ostream& os, SeqStack<T>& s) {
};
template <class T>
SeqStack<T>::SeqStack(int sz) : top(-1), maxSize(sz) {
elements = new T[maxSize];
assert(elements != NULL);
}
template <class T>
void SeqStack<T>::overflowProcess() {
T* newArray = new T[maxSize + stackIncreament];
if (newAway == NULL) {
cerr << "存储分配失败!" << endl;
exit(1);
}
for (int i = 0; i <= top; i++) {
newArray[i] = elements[i];
}
delete[] elements;
elements = newArray;
}
template <class T>
void SeqStack<T>::push(T& x) {
/*if (isFull()) {
overflowProcess();
}
elements[++i] = x; // also test i++*/
isFull() ? overflowProcess() : elements[++top] = x;
}
template <class T>
bool SeqStack<T>::pop(T& x) {
if (isEmpty()) {
return false;
}
x = *elements[top--]; // compare --top
return true;
}
template <class T>
bool SeqStack<T>::getTop(T& x) {
if (isEmpty()) {
return false;
}
x = elements[top];
return true;
}
#include <iostream>
#include "SeqStack.h"
using namespace std;
int main() {
int x = 1;
SeqStack<int> stack;
cout << stack.getSize() << endl;
cout<< stack.isFull() << endl;
stack.push(x);
cout << x <<endl;
}
编译器报错如下:
c:\documents and settings\longkai\my documents\visual studio 2010\projects\stack\stack\seqstack.h(45): 编译类 模板 成员函数“void SeqStack<T>::overflowProcess(void)”时
with
[
T=int
]
c:\documents and settings\longkai\my documents\visual studio 2010\projects\stack\stack\seqstack.h(39): 编译类 模板 成员函数“SeqStack<T>::SeqStack(int)”时
with
[
T=int
]
c:\documents and settings\longkai\my documents\visual studio 2010\projects\stack\stack\main.cpp(9): 参见对正在编译的类 模板 实例化“SeqStack<T>”的引用
with
[
T=int
]
一下是具体的程序,请大家帮帮忙!
#include <assert.h>
#include <iostream>
const int stackIncreament = 20;
template <class T>
class SeqStack {
private:
T* elements;
int top;
int maxSize;
void overflowProcess();
public:
SeqStack(int sz = 50);
~SeqStack() { delete[] elements; }
void push(T& x);
bool pop(T& x);
bool getTop(T& x);
bool isEmpty() { return top == -1 ? true : false; }
bool isFull() { return top == maxSize - 1 ? true : false; }
int getSize() { return top + 1; }
void makeEmpty() { top = -1; }
//friend ostream& operator << (ostream& os, SeqStack<T>& s) {
};
template <class T>
SeqStack<T>::SeqStack(int sz) : top(-1), maxSize(sz) {
elements = new T[maxSize];
assert(elements != NULL);
}
template <class T>
void SeqStack<T>::overflowProcess() {
T* newArray = new T[maxSize + stackIncreament];
if (newAway == NULL) {
cerr << "存储分配失败!" << endl;
exit(1);
}
for (int i = 0; i <= top; i++) {
newArray[i] = elements[i];
}
delete[] elements;
elements = newArray;
}
template <class T>
void SeqStack<T>::push(T& x) {
/*if (isFull()) {
overflowProcess();
}
elements[++i] = x; // also test i++*/
isFull() ? overflowProcess() : elements[++top] = x;
}
template <class T>
bool SeqStack<T>::pop(T& x) {
if (isEmpty()) {
return false;
}
x = *elements[top--]; // compare --top
return true;
}
template <class T>
bool SeqStack<T>::getTop(T& x) {
if (isEmpty()) {
return false;
}
x = elements[top];
return true;
}
#include <iostream>
#include "SeqStack.h"
using namespace std;
int main() {
int x = 1;
SeqStack<int> stack;
cout << stack.getSize() << endl;
cout<< stack.isFull() << endl;
stack.push(x);
cout << x <<endl;
}
作者: im_AK47 发布时间: 2011-10-29
template <class T>
void SeqStack<T>::overflowProcess() {
T* newArray = new T[maxSize + stackIncreament];
if (newAway == NULL) { // 一个简单的拼写错误,跟模板无关,呵呵
cerr << "存储分配失败!" << endl;
exit(1);
}
for (int i = 0; i <= top; i++) {
newArray[i] = elements[i];
}
delete[] elements;
elements = newArray;
}
void SeqStack<T>::overflowProcess() {
T* newArray = new T[maxSize + stackIncreament];
if (newAway == NULL) { // 一个简单的拼写错误,跟模板无关,呵呵
cerr << "存储分配失败!" << endl;
exit(1);
}
for (int i = 0; i <= top; i++) {
newArray[i] = elements[i];
}
delete[] elements;
elements = newArray;
}
作者: dizuo 发布时间: 2011-10-30
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28