std::sort (iterator, iterator, custom) will not compile

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".
1
2
3
encounter::encounter(){
// ... cut
std::sort (allpeople.begin(), allpeople.end(), sortByInit);}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
bool encounter::sortByInit (character& a, character& b) {
    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: { return true;}
            case 2: { return false;}
            default: {cout << "Invalid input please retry";}
          }
        }
    }
    else { return a.getinit() > b.getinit(); }
}
Last edited on
What is allpeople?
1
2
// bool encounter::sortByInit (character& a, character& b)
bool encounter::sortByInit ( const character& a, const character& b ) const // const-correct 


1
2
3
4
5
6
7
encounter::encounter(){
// ... cut
// std::sort (allpeople.begin(), allpeople.end(), sortByInit);
using namespace std::placeholders ; // #include <functional>
std::sort( allpeople.begin(), allpeople.end(),
           std::bind( &encounter::sortByInit, this, _1, _2 ) ) ;
}
Just overload the `()` operator for your class. Or make the sortByInit method static
I did what you recommended JLBorges. This is what I got
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <functional> // added
using namespace 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;
};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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 () );
    }
    using namespace 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: { return true;}
            case 2: { return false;}
            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.
> error: 'placeholders' is not a namespace-name|
> etc.

You need C++11. Compile with -std=c++11


> passing 'const character' as 'this' argument of 'int character::getinit()' discards qualifiers
> etc.

1
2
3
4
5
6
7
8
9
10
11
class character 
{
     // ....
     // int getinit() ;
     int getinit() const ; // const-correct

    // std::string character::getname() ;
    std::string character::getname() const ;
    
    // ...
};
Thank you, fixed.
Topic archived. No new replies allowed.