PDA

View Full Version : help -- removing items from QList container



drinkwater
28th January 2012, 02:57
Hi, this is my first time posting to this forum. I'm definitely a noob at both QT and c++, but with that said, I'm really liking both. Some of the details are killing me though. My problem is that I cannot delete an object from a QList Container. I can append just fine. Please see my code below.
---------------------------

class Contact{
public:
Contact(int cat, QString fn, QString ln,
QString sa, QString zc, QString cityv,QString pn ); //Contact cstor

int category;
QString firstName;
QString lastName;
QString streetAddress;
QString zipCode;
QString city;
QString phoneNumber;

public:
QString toString(){}
//I don't know what this method is for
};

class ContactList{ //define class
public:
ContactList(); //constructor defined

QList<Contact> List; //this holds everything -- QList container class

void add(Contact c){
List.append(c); //this works just fine!
}

void remove(Contact c){

List.removeAll(c); //this does not work <-------------this is my problem line!
}
};
---------------------
I've tried putting combinations of & and * on the parameters thinking maybe i need to use
a reference instead of actually passing an object, but nothing works. Thank you for any help
in this matter. I'm sure there is a very basic principle I'm missing, but as i said, I'm still learning.

oops, just noticed when i pasted text in here i have an extra "}" in there....that's just a typo and is not the cause of my problem. It definitely has to do with the fact that i'm trying to remove a class object from List

-Ryan

wysota
28th January 2012, 02:58
I think the problem is you are missing an implementation for the equals operator (operator==) for your class. QList is unable to compare items it holds.

drinkwater
28th January 2012, 03:21
I'm sorry, I don't know what that means...i feel a noob smack is coming. I put "operator==" text in which class and what does it do? Thanks for any info you can give me.

ChrisW67
28th January 2012, 05:24
operator==() is either a member function of Contact or a free function that compares two Contacts and return a true/false indication of equality.
The function prototype is one of these:


bool operator==(const Contact &rhs) { ... } // member function version
OR
bool operator==(const Contact &lhs, const Contact &rhs) { ... } // free function version


You want to look at "operator overloading" in your favourite C++ book. If you don't have one then try at Stack Overflow (http://stackoverflow.com/questions/4421706/operator-overloading) or http://www.mindview.net/Books/TICPP/ThinkingInCPP2e.html (there is a freely downloadable version).

drinkwater
28th January 2012, 16:03
Thank you very much! That worked.