
-----------------------------------
HazySmoke)345
Sat Sep 15, 2007 11:47 am

C++ stack malfunctioning?
-----------------------------------
The following code works perfectly:
#include 
#include 
using namespace std;
int main(){
	int a = 345; stack S;
	S.push(a);
	printf ("%d", S.top() == a);
return 0;}

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:
#include 
#include 
using namespace std;
typedef struct{int x, y;}point;
int main(){
	point p; stack S;
	p.x = 345; p.y = -345;
	S.push(p);
	printf ("%d", S.top() == p);
return 0;}


The error says:
testing.cpp:9: error: no match for 'operator==' in '(&S)->std::stack::top [with _Tp = point, _Sequence = std::deque]() == p'

I totally don't get it. Why does the stack.top() method work for one integer but not for a collection of two?

-----------------------------------
Mazer
Sat Sep 15, 2007 12:30 pm

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.

-----------------------------------
OneOffDriveByPoster
Sat Sep 15, 2007 5:22 pm

Re: 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.

Try std::pair (#include )--it's great.

-----------------------------------
md
Sat Sep 15, 2007 6:55 pm

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 struct point { int x, y; };

There is nothing I hate more then needless typedefs.

-----------------------------------
wtd
Sun Sep 16, 2007 2:43 am

RE:C++ stack malfunctioning?
-----------------------------------
Using std::pair would be the better option.
