This is homework, although it's already submitted with a different approach.
I'm getting the following from Visual Studio 2008
error C2893: Failed to specialize function template 'void std::sort(_RanIt,_RanIt,_Pr)'
The code is as follows
main.cpp Database<> db; db.loadDatabase(); db.sortDatabase(sort_by_title()); Database.cpp void Database<C>::sortDatabase(const sort_by &s) { std::sort(db_.begin(), db_.end(), s); }
And the function objects are defined as
struct sort_by : public std::binary_function<const Media *, const Media *, bool> { virtual bool operator()(const Media *l, const Media *r) const = 0; }; struct sort_by_title : public sort_by { bool operator()(const Media *l, const Media *r) const { ... } }; ...
What's the cure here?
[Edit] Sorry, maybe I should have made the inheritance clear
template <typename C = std::vector<Media *> > class Database : public IDatabase<C>
[/Edit]
[Edit2]
After the suggestion from Toolbox (which seemed very reasonable) I ended up with the following error message
error C2664: 'Database<>::sortMedia' : cannot convert parameter 1 from 'sort_by_title' to 'const sort_by &'
main.cpp is still the same, but with some slight modifications to the functor hierarchy and source files. Forward declarations and such did not work so I had to put the definitions in separate files.
Search.h struct sort_by_impl { virtual bool operator()(const Media *l, const Media *r) const = 0; }; struct sort_by : public std::binary_function<const Media *, const Media *, bool> { sort_by_impl *sbp; bool operator()(const Media *l, const Media *r) const { return (*sbp)(l, r); } }; IDatabase.h struct sort_by_title : public sort_by_impl { bool operator()(const Media *l, const Media *r) const { return (l->getTitle() < r->getTitle()); } };
I'm really not grokking this, what am I missing here? Some conversion operation, or what?
[/Edit2]
[Edit3]
Last and final edit, I hope. I actually got this working after debugging and rewriting some of the code. This is what I ended up with, and it's the best I could do
class sort_by : public std::binary_function<const Media *, const Media *, bool> { public: sort_by(sort_by_impl *sbp) : sbp_(sbp) {}; bool operator()(const Media *l, const Media *r) const { return (*sbp_)(l, r); } private: sort_by_impl *sbp_; }; main.cpp db.sortDatabase(&sort_by_title()); Database.cpp void Database<C>::sortDatabase(const sort_by &s) { std::sort(db_.begin(), db_.end(), s);
This seems to work, both in a separate project (spending the better part of this day messing with this) and in my actual project which I submitted some days ago.
Thank you very much for your time and help!
[/Edit3]