Troubles with a stack

I am working on an operator overloading function for my stack to allow two stacks to be compared and return a boolean value. Here is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
129 // Compare the values, and return a bool result.
130 template <class T> bool Stack<T>::operator==(const Stack<T>& rhs) const {
131	bool equal = true;
132	if (this->getSize() != rhs->getSize()){
133		return !equal;
134	}
135	this->cursor = this->end;
136	rhs->cursor = rhs->end;
137	
138	for (int itr = 0; itr < rhs->getSize(); itr++){
139		if (this->cursor.data == rhs->cursor.data){
140			cursor = cursor.prev;
141			rhs->cursor = rhs->cursor.prev;
142		}
143		else
144			return !equal;
145	}
146	return equal;
147 }


My errors are:

1
2
3
4
5
6
7
8
9
10
11
12
src/Stack.h:132: error: base operand of ‘->’ has non-pointer type ‘const Stack<int>’
src/Stack.h:132: error: passing ‘const Stack<int>’ as ‘this’ argument of ‘unsigned int Stack<T>::getSize() [with T = int]’ discards qualifiers
src/main.cpp:24:   instantiated from here
src/Stack.h:135: error: assignment of data-member ‘Stack<int>::cursor’ in read-only structure
src/Stack.h:136: error: base operand of ‘->’ has non-pointer type ‘const Stack<int>’
src/Stack.h:136: error: base operand of ‘->’ has non-pointer type ‘const Stack<int>’
src/Stack.h:138: error: base operand of ‘->’ has non-pointer type ‘const Stack<int>’
src/Stack.h:139: error: base operand of ‘->’ has non-pointer type ‘const Stack<int>’
src/Stack.h:139: error: request for member ‘data’ in ‘((const Stack<int>*)this)->Stack<int>::cursor’, which is of non-class type ‘Node<int>* const’
src/Stack.h:140: error: request for member ‘prev’ in ‘((const Stack<int>*)this)->Stack<int>::cursor’, which is of non-class type ‘Node<int>* const’
src/Stack.h:141: error: base operand of ‘->’ has non-pointer type ‘const Stack<int>’
src/Stack.h:141: error: base operand of ‘->’ has non-pointer type ‘const Stack<int>’
Last edited on
Instead of returning 'equal' and '!equal', can't you just return true or false?

Anyway, rhs is a reference, so you use '.', not '->'.
On Lines 138-145, I am not sure why you have a for loop, just use a while loop.
Thanks for the reply. I fixed the for loop on my post to read correctly. I used 'bool equal' to make it easier to read the code.

I made the change for rhs, code now reads:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
129  // Compare the values, and return a bool result.
130  template <class T> bool Stack<T>::operator==(const Stack<T>& rhs) const {
131	 bool equal = true;
132	 if (this->getSize() != rhs.getSize()){
133		return !equal;
134	 }
135	 this->cursor = this->end;
136	 rhs.cursor = rhs.end;
137	
138	 for (int itr = 0; itr < rhs.getSize(); itr++){
139		if (this->cursor.data == rhs.cursor.data){
140			cursor = cursor.prev;
141			rhs.cursor = rhs.cursor.prev;
142		}
143		else
144	 		return !equal;
145	 }
146	 return equal;
147 }



And my errors read:
1
2
3
4
5
6
7
8
9
10
11
12
13
src/Stack.h:132: error: passing ‘const Stack<int>’ as ‘this’ argument of ‘unsigned int Stack<T>::getSize() [with T = int]’ discards qualifiers
src/main.cpp:24:   instantiated from here
src/Stack.h:132: error: passing ‘const Stack<int>’ as ‘this’ argument of ‘unsigned int Stack<T>::getSize() [with T = int]’ discards qualifiers
src/main.cpp:24:   instantiated from here
src/Stack.h:135: error: assignment of data-member ‘Stack<int>::cursor’ in read-only structure
src/Stack.h:136: error: assignment of data-member ‘Stack<int>::cursor’ in read-only structure
src/Stack.h:138: error: passing ‘const Stack<int>’ as ‘this’ argument of ‘unsigned int Stack<T>::getSize() [with T = int]’ discards qualifiers
src/main.cpp:24:   instantiated from here
src/Stack.h:138: warning: comparison between signed and unsigned integer expressions
src/Stack.h:139: error: request for member ‘data’ in ‘rhs->Stack<int>::cursor’, which is of non-class type ‘Node<int>* const’
src/Stack.h:139: error: request for member ‘data’ in ‘((const Stack<int>*)this)->Stack<int>::cursor’, which is of non-class type ‘Node<int>* const’
src/Stack.h:140: error: request for member ‘prev’ in ‘((const Stack<int>*)this)->Stack<int>::cursor’, which is of non-class type ‘Node<int>* const’
src/Stack.h:141: error: request for member ‘prev’ in ‘rhs->Stack<int>::cursor’, which is of non-class type ‘Node<int>* const
lines 135/136, rhs and this are const, so you can't modify cursor.
for 139-141, you probably can't access the data because you could possibly modify it (and const functions aren't allowed to)

Can I see main.cpp and probably Stack's constructor?
Line 132: this and rhs are const, but getSize() requires that the this parameter be non-const. Make getSize() a const function.
Line 135: const function is attempting to change the value of member variables.
Line 136: Attempting to change the value of member variables of a constant object.
Line 139: cursor is a pointer, not an object. Use the -> operator to access its members.
Lines 140 and 141: Again trying to change values of member variables of constant objects.
Obviously I don't have * and -> down solid yet. Anyhow it works great now that I got that cleared up and I got the const issue fixed. Thanks for the comments.
Topic archived. No new replies allowed.