c++ - Vector sorting with different objects in it -
i trying come out sort function, previously, help, manage sort sorts base on variable stored object vector.
pointtwod object.
bool compare(const pointtwod& a, const pointtwod& b) { return a.getcivindex() > b.getcivindex(); //sort high low } //to sort, have call in function void sort(vector<pointtwod>& vector) { sort(vector.begin(), vector.end(), compare); }
base on this, tried recreate it.
shapetwod object now, parent class. have 3 sub classes polymorphism store sub class objects vector.
bool compareascend(shapetwod& a, shapetwod& b) { return b.getarea() > a.getarea(); } bool comparedescend(shapetwod& a, shapetwod& b) { return a.getarea() > b.getarea(); } //if compile this, compiler fine void sort(vector<shapetwod*>& vector) { string choice; cout << "\n\na)\tsort area (ascending)" << endl; cout << "b)\tsort area (descending)" << endl; cout << "c)\tsort special type , area" << endl; cout << "\tplease select sort option (‘q’ go main menu): "; cin >> choice; transform(choice.begin(), choice.end(), choice.begin(), ::tolower); if (choice == "a") { sort(vector.begin(), vector.end(), compareascend); //these lines giving error } else if (choice == "b") { sort(vector.begin(), vector.end(), comparedescend); //these lines giving error } }
but when try compile, compiler give me load of errors, don't understand.
when trying sort vector
containing shapetwod*
s, compare function must work shapetwod*
too, not shapetwod&
or shapetwod const&
.
change
bool compareascend(shapetwod& a, shapetwod& b) { return b.getarea() > a.getarea(); }
to
bool compareascend(shapetwod* a, shapetwod* b) { return b->getarea() > a->getarea(); }
change comparedescend
similarly.
Comments
Post a Comment