comparing stacks
ironman (20)Jan 6, 2009 at 3:25am UTC
How do you overload the == operator in a stack?
Zaita (1560)Jan 6, 2009 at 3:25am UTC
I would imagine, the same way you override all other operators. But making sure it's a global operator the same as the original ==. The question is, why would you want to override it?
ironman (20)Jan 6, 2009 at 3:25am UTC
i need to be able to see if stack1 == stack2
Last edited on Jan 6, 2009 at 3:25am UTC
Zaita (1560)Jan 6, 2009 at 3:25am UTC
There is already an operator for this.
ironman (20)Jan 6, 2009 at 3:25am UTC
yes but the assignment asks for me to overload the == operator so that i can compare 2 stacks
Zaita (1560)Jan 6, 2009 at 3:25am UTC
1 2 3
bool operator ==(const stack&, const stack&) {
}
ironman (20)Jan 6, 2009 at 3:25am UTC
lol i know how to overload the == operator in a general program, but you need templates in a stack and you cant overload the == the same way i need to be able to compare stack1 (which contains 1,2,3) to stack2 (which contains 2,3,4,5) and so on
Zaita (1560)Jan 6, 2009 at 3:25am UTC
Surely you could've figured it out. It's just the same.1 2 3 4
template <typename T>
bool operator ==(const stack<T>& stack1, const stack<T>& stack2) {
return false ;
}
ironman (20)Jan 6, 2009 at 3:25am UTC
yeah i have the syntax figured out, im just having trouble writing the definition. i cant seem to figure out how to compare the size of the stack and the elements of the stack
ironman (20)Jan 6, 2009 at 3:25am UTC
neither of those showed me how to define the the function
Zaita (1560)Jan 6, 2009 at 3:25am UTC
That's what your suppose to come up with yourself. We aren't here to give you answers to your homework.
ironman (20)Jan 6, 2009 at 3:25am UTC
my professor instructed us to go online and look for how to do it
Zaita (1560)Jan 6, 2009 at 3:25am UTC
Then start digging through the source code to your compiler's STL. Have a look at how it's done there.
This topic is archived - New replies not allowed.