Hey, it's me again. Again with a topic that has been covered here but reading through the old threads didnt help me to solve my problem. And I am pretty clueless at the moment.
Consider the class passenger_queue with 2 functions, namely:
int passenger_queue::get_passengers_waiting() const
{
// Insert your code here. Use the iterator defined in this class.
int sum = 0;
}
return sum;
}
and
std::vector<passenger*>& passenger_queue::passengers_at_floor(int floor_)
{
return _waiting_passengers[floor_];
}
member variables are:
typedef std::map<int, std::vector<passenger*> > p_map;
p_map _waiting_passengers;
std::vector<passenger> _passengers;
First of all the comment in the get_passengers_waiting. Apart from the constructor which I didnt include in the code, there is no iterator defined in this class (or else I dont know how an iterator definition looks like).
So my first solution is the following (which seems to work):
int passenger_queue::get_passengers_waiting() const
{
// Insert your code here. Use the iterator defined in this class.
int sum = 0;
for (p_map::const_iterator it = _waiting_passengers.begin(); it != _waiting_passengers.end(); ++it) {
sum+= it->second.size();
}
return sum;
}
I dont need the function passengers_at_floor at all. Now I think the idea would be to use that function but then I run into trouble - passing const as this discards qualifiers. hence the title. What I try is the following:
int passenger_queue::get_passengers_waiting() const
{
// Insert your code here. Use the iterator defined in this class.
int sum = 0;
for (p_map::const_iterator it = _waiting_passengers.begin(); it != _waiting_passengers.end(); ++it) {
sum+= passengers_at_floor((it->first)).size();
}
return sum;
}
which gives me the error:
error: passing ‘const passenger_queue’ as ‘this’ argument of ‘std::vector<passenger*, std::allocator<passenger*> >& passenger_queue::passengers_at_floor(int)’ discards qualifiers
I tried to use iterator instead of const_iterator but that doesnt go down well with the const in the get_passengers_waiting(). So honestly I wouldnt know how to get to the correct result with the use of the passengers_at_floor().
Can anyone point me in the right direction?
I appreciate any help and I hope I wasnt too messy.
(If mike is reading this - I hear you, no leading _ but its still from the same assignment paper :))