Reputation: 267
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() << " ";
}
}
Upvotes: 3
Views: 18474
Reputation: 1
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;
}
Upvotes: 0
Reputation: 35440
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
Upvotes: 9
Reputation: 3860
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.
Upvotes: 2
Reputation: 171
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.
Upvotes: 7