I am trying to use std::sort to sort a vector of complex objects using a custom function. However, it keeps erroring "Unresolved overloaded function type".
// bool encounter::sortByInit (character& a, character& b)
bool encounter::sortByInit ( const character& a, const character& b ) const // const-correct
#include <iostream>
#include <string>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <functional> // added
usingnamespace std;
class character {...}
class encounter{
public:
encounter (character*, int); // Constructor
void order_by_init (); // Order all players by initiative, highest to lowest
private:
int ENEMIES; // number of enemies in this encounter
vector<character> allpeople; // vector of all people in encounter
bool sortByInit (const character&,const character&) const;
};
encounter::encounter (character player_in[], int size ){
banner ("NEW ENCOUNTER", "*");
for (int i = 0; i < size; i++ )
{
allpeople.push_back(player_in[i] );
}
cout << "How many enemies?";
cin >> ENEMIES;
for (int i = 0; i < ENEMIES; i++ )
{
allpeople.push_back(character () );
}
usingnamespace std::placeholders ; // added, but giving me errors
// changed, but giving me errors
sort (allpeople.begin(), allpeople.end(), std::bind( &encounter::sortByInit, this, _1, _2 ) );
}
bool encounter::sortByInit (const character& a,const character& b) const {
if (a.getinit () == b.getinit ()) {
int input = 0;
while (input != 1 || input != 2) {
cout<< a.getname() << " and " << b.getname() << " have the same initiative.\n"
<< "Please reroll and input who is higher.\n"
<< "1. " << a.getname() << "\n2. " << b.getname();
cin >> input;
switch (input) {
case 1: { returntrue;}
case 2: { returnfalse;}
default: {cout << "Invalid input please retry";}
}
}
}
else { return a.getinit() > b.getinit(); }
}
Error Messages (note: code lines are not correct due to cutting portions of the code.)
||=== Build: Linked list in Linked list (compiler: GNU GCC Compiler) ===|
Linked-List Thread.cpp||In constructor 'encounter::encounter(character*, int)':|
Linked-List Thread.cpp|214|error: 'placeholders' is not a namespace-name|
Linked-List Thread.cpp|214|error: expected namespace-name before ';' token|
Linked-List Thread.cpp|215|error: 'bind' is not a member of 'std'|
Linked-List Thread.cpp|215|error: '_1' was not declared in this scope|
Linked-List Thread.cpp|215|error: '_2' was not declared in this scope|
Linked-List Thread.cpp||In member function 'bool encounter::sortByInit(const character&, const character&) const':|
Linked-List Thread.cpp|218|error: passing 'const character' as 'this' argument of 'int character::getinit()' discards qualifiers [-fpermissive]|
Linked-List Thread.cpp|218|error: passing 'const character' as 'this' argument of 'int character::getinit()' discards qualifiers [-fpermissive]|
Linked-List Thread.cpp|221|error: passing 'const character' as 'this' argument of 'std::string character::getname()' discards qualifiers [-fpermissive]|
Linked-List Thread.cpp|221|error: passing 'const character' as 'this' argument of 'std::string character::getname()' discards qualifiers [-fpermissive]|
Linked-List Thread.cpp|223|error: passing 'const character' as 'this' argument of 'std::string character::getname()' discards qualifiers [-fpermissive]|
Linked-List Thread.cpp|223|error: passing 'const character' as 'this' argument of 'std::string character::getname()' discards qualifiers [-fpermissive]|
Linked-List Thread.cpp|233|error: passing 'const character' as 'this' argument of 'int character::getinit()' discards qualifiers [-fpermissive]|
Linked-List Thread.cpp|233|error: passing 'const character' as 'this' argument of 'int character::getinit()' discards qualifiers [-fpermissive]|
Linked-List Thread.cpp|234|warning: control reaches end of non-void function [-Wreturn-type]|
||=== Build failed: 13 error(s), 1 warning(s) (0 minute(s), 0 second(s)) ===|
if you want any other code let me know.
@Smac What would the overloaded () look like? And my understanding of method functions (it is probably wrong) is that they are in essence already static in that they are not tied to each instance of the class but are called and used for all instances of the class as needed. Please correct me if I am wrong.