Remove Object from C++ list

13,338

Solution 1

The issue is, as pointed out, that Student is lacking an operator== that is required by std::list::remove.

#include <string>
class Student {
    std::string name; 
    int id; 

public:
    bool operator == (const Student& s) const { return name == s.name && id == s.id; }
    bool operator != (const Student& s) const { return !operator==(s); }
    void setValues(std::string, int); 
    std::string getName();
    Student() : id(0) {}
};

Note how both operator== and operator != are overloaded. It is expected that if two objects can be compared with ==, then != should also be available to be used. Check how operator!= is written in terms of operator ==.

Also note that the parameter is passed as a const reference, and the functions themselves are const.

Live Example: http://ideone.com/xAaMdB

Solution 2

std::list::remove() removes all elements in the list that compare equal to the element you give. You don't give your definiton of Student, but likely you don't have an operator == () method defined so the call to remove() cannot work.

Solution 3

The list can't delete your student cause it can't know how to compare the students in the list to the one given to the remove method.
Note that the student is passed by value and therefore is a different instance than the one in the list.

One thing you can do is implement an operator== in Student which will help the list find your student.

Another possibility (especially relevant if you can't change Student class) will be to hold a list of Student* (Student pointer), and then the list will be able to compare the pointers, and find the one you're trying to remove.

Share:
13,338
user3281388
Author by

user3281388

Updated on June 07, 2022

Comments

  • user3281388
    user3281388 almost 2 years

    I'm new at C++... I'm making some classes - one for Student and one for Courses. There is an "list" inside of Courses that adds Student Objects.

    I am able to add a student:

    void Course::addStudent(Student student)
    {
        classList.push_back(student); 
    }
    

    But when I go to delete a Student, I'm not able to remove it. I'm getting a long error about Student not be derived and something about the operator==(const allocator).

    void Course::dropStudent(Student student)
    {
         classList.remove(student); 
    }
    

    Any suggestions? Thanks!!

    I was referring to this website for how to add/remove elements: http://www.cplusplus.com/reference/list/list/remove/

    Student Code:

    class Student {
    std::string name; 
    int id; 
    public:
    void setValues(std::string, int); 
    std::string getName();
    };
    
    void Student::setValues(std::string n, int i)
    {
    name = n; 
    id = i; 
    };
    
    std::string Student::getName()
    {
        return name; 
    }
    

    Full Course code:

    class Course 
    {
    std::string title; 
    std::list<Student> classList; //This is a List that students can be added to. 
    std::list<Student>::iterator it; 
    
    public: 
    void setValues(std::string); 
    void addStudent(Student student);
    void dropStudent(Student student);
    void printRoster();
    };
    void Course::setValues(std::string t)
    {
        title = t;  
    };
    
    void Course::addStudent(Student student)
    {
        classList.push_back(student); 
    }
    
    void Course::dropStudent(Student student)
    {
        classList.remove(student);
    }
    
    void Course::printRoster()
    {
        for (it=roster.begin(); it!=roster.end(); ++it)
        {
            std::cout << (*it).getName() << " "; 
        }
    }
    
  • user3281388
    user3281388 about 9 years
    Should I make a compare() method that would use the operator == ( ) then call that method in my remove(compare())?
  • user3281388
    user3281388 about 9 years
    Is it best to create a separate method of the operator== or would it be okay put it inside my remove method?
  • Oldcat
    Oldcat about 9 years
    You could, but you could just define a method in the class to compare two Student classes - bool Student::operator == (Student &) and decide what makes two students equal on that basis
  • PaulMcKenzie
    PaulMcKenzie about 9 years
    @user3281388 The list::remove will use == to compare two objects. It knows nothing nor will it do anything wth your compare() function. If you want to write a compare() function to be used by yourself or other functions, then that's a different story.
  • Arnon Zilca
    Arnon Zilca about 9 years
    the operator== should be a part of the Student class. Usually it's being implemented outside the class though. check out this link.
  • user3281388
    user3281388 about 9 years
    Thanks so much Paul! This works! I was starting to work on adding an operator, but then saw your comment. I was close, but not quite. I really appreciate your help! Thank you!
  • user3281388
    user3281388 about 9 years
    Thanks for your think Arnon, that helped me understand what was going on!
  • Nasir Iqbal
    Nasir Iqbal over 4 years
    Thanks for the answer, Can you please edit your answer to elaborate it a little bit? Like what op was doing wrong, and how you fixed it!