U
utab
Dear all,
In a question in the highly recommended book Accelerated C++, it is asked
to change a const function into a plain function. After on the reader is
expected to find which function(s) should change.
The function to change is
original version
std::string name() const { return n; }
And this function is called from a predicate function which is passed to
a sort for vector:
original version
bool compare( const Student_info& x, const Student_info& y)
{
return x.name() < y.name();
}
and somewhere in the code sort is called for vector
sort(vec.begin(),vec.end(),compare)
Turning the name into a plain non-const function requires the change of
the const parameters of the compare function because const objects can
call const member functions. But making this change does not solve my
problem. I am getting an error(which is not that helpful from g++) from
the sort function and compare predicate. If someone wants the whole
code(might be a bit long though), I can paste it but I wanted to ask it
directly from the above explanation.
Can somebody help me to figure this problem out?
In a question in the highly recommended book Accelerated C++, it is asked
to change a const function into a plain function. After on the reader is
expected to find which function(s) should change.
The function to change is
original version
std::string name() const { return n; }
And this function is called from a predicate function which is passed to
a sort for vector:
original version
bool compare( const Student_info& x, const Student_info& y)
{
return x.name() < y.name();
}
and somewhere in the code sort is called for vector
sort(vec.begin(),vec.end(),compare)
Turning the name into a plain non-const function requires the change of
the const parameters of the compare function because const objects can
call const member functions. But making this change does not solve my
problem. I am getting an error(which is not that helpful from g++) from
the sort function and compare predicate. If someone wants the whole
code(might be a bit long though), I can paste it but I wanted to ask it
directly from the above explanation.
Can somebody help me to figure this problem out?