PDA

View Full Version : QList Overloading operator==()



josepvr
26th January 2009, 22:44
Hi!
I've a list of a custom items and I have implemented function == in my code but it doesn't works!

This is my code:



#ifndef RUTA_H
#define RUTA_H

#include <QtCore>

class Ruta
{
public:
Ruta();
void setNum(int r);
int getNum();
bool operator ==(const Ruta & r) const;

private:
int num;
};
#endif // RUTA_H




#include "ruta.h"

Ruta::Ruta()
{
}

void Ruta::setNum(int r)
{
num = r;
}

int Ruta::getNum()
{
return num;
}

bool Ruta::operator ==(const Ruta & r) const
{
return(r.num == this->num);
}


The main function is:



Ruta *a = new Ruta();
Ruta *b = new Ruta();

a->setNum(1);
b->setNum(1);

if(a == b)
qDebug("ok!");
else
qDebug("no");



And the output is:

no

I don't know what can I do, because I need It to use a "indexOf" function in a QList<Ruta*>

josepvr
26th January 2009, 23:04
I've tried this, but doesn't works...:crying:


bool operator==( Ruta *r1)
{
return r1->getNum() == this->getNum();
}

wysota
26th January 2009, 23:17
Hi!
I've a list of a custom items and I have implemented function == in my code but it doesn't works!

This is my code:



#ifndef RUTA_H
#define RUTA_H

#include <QtCore>

class Ruta
{
public:
Ruta();
void setNum(int r);
int getNum();
bool operator ==(const Ruta & r) const;

private:
int num;
};
#endif // RUTA_H




#include "ruta.h"

Ruta::Ruta()
{
}

void Ruta::setNum(int r)
{
num = r;
}

int Ruta::getNum()
{
return num;
}

bool Ruta::operator ==(const Ruta & r) const
{
return(r.num == this->num);
}


The main function is:



Ruta *a = new Ruta();
Ruta *b = new Ruta();

a->setNum(1);
b->setNum(1);

if(a == b)
qDebug("ok!");
else
qDebug("no");



And the output is:

no

I don't know what can I do, because I need It to use a "indexOf" function in a QList<Ruta*>

You are comparing pointers to items, not items themselves. Either make a QList<Ruta> and not QList<Ruta*> or provide a custom operator working on pointers or compare items and not pointers:

(*a)==(*b)

josepvr
27th January 2009, 08:03
I need a pointer list, QList<Ruta *>.

how should be this function to work with a pointer without doing (*a)==(*b)?


bool Ruta::operator ==(const Ruta & r) const
{
return(r.num == this->num);
}



Thanks!

wysota
27th January 2009, 08:23
bool operator==(Ruta *one, Ruta *two){
return one->num==two->num;
}

josepvr
27th January 2009, 14:38
I've a compilation error.

I've tried this:


#ifndef RUTA_H
#define RUTA_H

#include <QtCore>

class Ruta
{
public:
Ruta();
void setNum(int r);
int getNum();

bool operator==(Ruta *one, Ruta *two)
{
return one->num==two->num;
}

private:
int num;
};
#endif // RUTA_H

I got this error:

D:/ruta.h:38: error: `bool Ruta::operator==(Ruta*, Ruta*)' must take exactly one argument

And then I've tried this:


#ifndef RUTA_H
#define RUTA_H

#include <QtCore>

class Ruta
{
public:
Ruta();
void setNum(int r);
int getNum();

private:
int num;
};

bool operator==(Ruta *one, Ruta *two)
{
return one->getNum() == two->getNum();
}

#endif // RUTA_H


i've another error:

D:/ruta.h:43: error: `bool operator==(Ruta*, Ruta*)' must have an argument of class or enumerated type

Thanks!

jpn
27th January 2009, 14:46
Unfortunately, if both operator==() parameters are pointers, it will be a pure comparison of pointer addresses and will not use the user-defined conversion:

C++ FAQ Lite: [26.10] Can I define an operator overload that works with built-in / intrinsic / primitive types? (http://www.parashift.com/c++-faq-lite/intrinsic-types.html#faq-26.10)

wysota
27th January 2009, 15:19
Hmm... right... in that case the easiest way is to keep the asterisks or get rid of pointers :)

josepvr
28th January 2009, 15:28
Thanks!
I've solved creating a function like this:

bool equals(Ruta *r1,Ruta *r2);
and another custom function to shearch this object into a list.

Thanks everybody!