Computer Science Canada C++ stack malfunctioning? |
Author: | HazySmoke)345 [ Sat Sep 15, 2007 11:47 am ] | ||||
Post subject: | C++ stack malfunctioning? | ||||
The following code works perfectly:
It makes sense. I pushed the value 345 into a stack and compared that value immediately to a. The following code, even though it's very similar to above, doesn't work:
The error says: testing.cpp:9: error: no match for 'operator==' in '(&S)->std::stack<_Tp, _Sequence>::top [with _Tp = point, _Sequence = std::deque<point, std::allocator<point> >]() == p' I totally don't get it. Why does the stack.top() method work for one integer but not for a collection of two? |
Author: | Mazer [ Sat Sep 15, 2007 12:30 pm ] |
Post subject: | RE:C++ stack malfunctioning? |
Have you tried defining the == operator for your new structure? I could be wrong, but I don't think it's supposed to provide one by default. |
Author: | OneOffDriveByPoster [ Sat Sep 15, 2007 5:22 pm ] |
Post subject: | Re: RE:C++ stack malfunctioning? |
Mazer @ Sat Sep 15, 2007 12:30 pm wrote: Have you tried defining the == operator for your new structure? I could be wrong, but I don't think it's supposed to provide one by default.
Try std::pair<int, int> (#include <utility>)--it's great. |
Author: | md [ Sat Sep 15, 2007 6:55 pm ] | ||
Post subject: | RE:C++ stack malfunctioning? | ||
IIRC the compiler will generate a compare function for any user defined types. What exactly does std::stack<>::top() return? Perhaps it's not returning what you think... What's wrong with
There is nothing I hate more then needless typedefs. |
Author: | wtd [ Sun Sep 16, 2007 2:43 am ] |
Post subject: | RE:C++ stack malfunctioning? |
Using std::pair<T> would be the better option. |