Facing Problem with operaror == in stl queue

P

pavan734

Hi,
I need small help. Suppose if u have two stl queues like

queue<int> q1,q2 ;
Then u can compare the two queues like

if(q1 ==q2)
{
.........
}

But Suppose if the queue is templatized over user class like
queue <user_class> q1,q2 ;

Then the statement
if(q1 ==q2) ...... is not working.

What should I make in the user_class for the above statement to work.
I think I have to overload == operator. Can anyone help me.
 
M

Marco Wahl

Hi,
I need small help. Suppose if u have two stl queues like

queue<int> q1,q2 ;
Then u can compare the two queues like

if(q1 ==q2)
{
.........
}

But Suppose if the queue is templatized over user class like
queue <user_class> q1,q2 ;

Then the statement
if(q1 ==q2) ...... is not working.

What should I make in the user_class for the above statement to work.
I think I have to overload == operator. Can anyone help me.

Yes. Define operator== for user_class. AFAICS you are
on the right track.
 
P

pavan734

Thank you. But its not working. Tell me if the following is right way
of overloading
class user_class{
int i ;
float j ;
public:
bool operator==( user_class ob2)
{
if(i == ob2.i && j == ob2.j)
{
return true ;
}
return false ;
}
}
did the samething. But still its not working
 
F

ferdinand.stefanus

Thank you. But its not working. Tell me if the following is right way
of overloading
class user_class{
int i ;
float j ;
public:
bool operator==( user_class ob2)
{
if(i == ob2.i && j == ob2.j)
{
return true ;
}
return false ;
}
}
did the samething. But still its not working

You should change

bool operator==( user_class ob2);

to

bool operator== ( const user_class& ob2) const;

And BTW, it's never a good idea to compare two floating points using ==
operator (http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.17)

Hope that helps.
 
P

Pete Becker

And BTW, it's never a good idea to compare two floating points using ==
operator (http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.17)

It would be a good idea to read the FAQ before citing it. It certainly
doens't say "it's never a good idea...". And that's because it often is.
The alternatives posted on newsgroups have their own set of surprises,
and the one in the FAQ has the curious property that it isn't
transitive: is_equal(a,b) and is_equal(b,c) does not imply
is_equal(a,c). But that discussion's been done to death.
 
R

Reginald Blue

Pete said:
It would be a good idea to read the FAQ before citing it. It certainly
doens't say "it's never a good idea...".

Actually it says nearly exactly that... but perhaps it shouldn't, as it is
quite misleading.

"Bottom line: Never use == to compare two floating point numbers."

--
Reginald Blue
"I have always wished that my computer would be as easy to use as my
telephone. My wish has come true. I no longer know how to use my
telephone."
- Bjarne Stroustrup (originator of C++) [quoted at the 2003
International Conference on Intelligent User Interfaces]
 
F

ferdinand.stefanus

I stand corrected. I googled the newsgroup, and there are indeed
situations where the built-in operator== is appropriate for floating
point.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top