Need some help with my stack class

This is what I have so far in writing my class stack, in the reverse function I keep getting an error that says " no operator "[]" matches these operands operand types are: stack [int]"

I am simply trying to make another stack to push all of the elements of the first stack onto to reverse their order, but I keep getting that error when I hover over the "[i]" in the reverse function.

Can anyone help me to correct this or let me know of a better way to reverse the order of the stack using only the push and pop functions?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <iostream>
using namespace std;

class stack {
public:
	int MAX_SIZE;
	stack(int MAX_SIZE = 10);
	~stack() { delete[] newStack;}
	void push(double x);
	double pop();
	void display();
	void reverse();
	


private:
	int top;
	double *newStack;
	double *tempStack;
	int tipTop = MAX_SIZE - 1;


};

stack::stack(int MAX_SIZE) {
		
		top = -1;
		newStack = new double[MAX_SIZE];
		tempStack = new double[MAX_SIZE];

}

void stack::push(double x) {

	if (top == tipTop) {
		cout << "Error: Stack is full!" << endl;
		return;
	}
	else {
	
		newStack[++top] = x;
	
	}
}

double stack::pop() {

	if (top == -1) {
		cout << "Error: Stack is empty!" << endl;
		return;
	}
	else {
	
		return newStack[top--];
	
	}


}

void stack::display() {

	cout << "Top -->  ";
	for (int i = top; i >= 0; i--) {
	
		cout << newStack[i] << endl << endl;

	}

}


void stack::reverse() {

	stack s1 = newStack[MAX_SIZE];
	stack s2 = tempStack[MAX_SIZE];

	for (int i = top; i >= 0; i--) {
	
		s2[i].push(s1[i]);
		s1[i].pop();

	}

}


Last edited on

I am simply trying to make another stack to push all of the elements of the first stack onto to reverse their order, but I keep getting that error when I hover over the "[i]" in the reverse function.

That's probably because s1 and s2 are not arrays, they are two single stack instances.

Last edited on
Topic archived. No new replies allowed.