Can anyone explain why I can't sort this vector:
#include <iostream>
#include <vector>
#include <algorithm>
#include <memory>
struct Test
{
public:
Test(const unsigned int input) : a(input){}
int a;
};
struct SortFunctor
{
bool operator()(std::shared_ptr<Test>& object1, std::shared_ptr<Test>& object2)
{
return(object1->a < object2->a);
}
};
//////////////////////////
int main (int argc, char *argv[])
{
srand(time(NULL));
std::vector<std::shared_ptr<Test> > objects;
std::shared_ptr<Test> object1(new Test(rand()%50));
objects.push_back(object1);
std::shared_ptr<Test> object2(new Test(rand()%50));
objects.push_back(object2);
std::sort(objects.begin(), objects.end(), SortFunctor());
return 0;
}
http://ideone.com/LoNoC - it complains "no known conversion for argument 1 from ‘const std::shared_ptr<Test>’ to ‘std::shared_ptr<Test>&’
It works if I changie the functor operator() to be const std::shared_ptr<Test>, but can't you always pass a mutable object to a function expecting a const object?
Thanks,
David