3

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() << " "; 
    }
}
4
  • We are going to need more detail. We don't even know what type of object classList is, or what the remove function looks like. Commented Mar 31, 2015 at 21:27
  • Sorry for the confusion, I have edited my doc. Hopefully it is more clear. Commented Mar 31, 2015 at 21:32
  • 2
    Please post the exact error messages -- do not describe them. Also we also need to see what Student consists of. If classList is a std::list, then remove() requires that Student requires an equality operator (operator ==) Commented Mar 31, 2015 at 21:32
  • Pass those students and strings by const reference or you copy them lots of times. Commented Mar 31, 2015 at 21:43

4 Answers 4

9

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

Sign up to request clarification or add additional context in comments.

1 Comment

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!
7

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.

3 Comments

Should I make a compare() method that would use the operator == ( ) then call that method in my remove(compare())?
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
@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.
2

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.

3 Comments

Is it best to create a separate method of the operator== or would it be okay put it inside my remove method?
the operator== should be a part of the Student class. Usually it's being implemented outside the class though. check out this link.
Thanks for your think Arnon, that helped me understand what was going on!
0
void Course::dropStudent(Student student)
{
    list<Student>::iterator itr=classList.begin();
    list<Student>temporary;
    while(itr!=classList.end())
    {
        if(itr->id!=student.id)
        {
            temporary.push_back(itr);
        }
        itr++;
    }
    classList=temporary;
}

1 Comment

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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.