0

I am currently attempting to use std::sort() in C++ to sort a vector of const char* vectors. Here is my current code:

std::vector<std::vector<const char*>> PlayerManager::GetSortedHighscores()
{
    std::vector<std::vector<const char*>> sortedHighscores;

    std::fstream hsFileIStream;

    hsFileIStream.open("Highscores.csv", std::ios::in);

    if (hsFileIStream.fail())
    {
        return sortedHighscores;
    }

    std::string line;

    while (std::getline(hsFileIStream, line))
    {
        std::vector<const char*> lineV;
        std::stringstream s(line);
        std::string data;

        while (std::getline(s, data, ','))
        {
            lineV.push_back(data.c_str());
        }

        sortedHighscores.push_back(lineV);
    }

    std::sort(sortedHighscores.begin(), sortedHighscores.end(), CompareRecord);

    return sortedHighscores;
}

bool PlayerManager::CompareRecord(std::vector<const char*>& record1, std::vector<const char*>& record2)
{
    const int MOVE_INDEX = 3;
    return (record1[MOVE_INDEX] < record2[MOVE_INDEX]);
}

However I am receiving the following errors:

'PlayerManager::CompareRecord': non-standard syntax; use '&' to create a pointer to member

'std::sort': no matching overloaded function found

'void std::sort(const _RanIt,const _RanIt)': expects 2 arguments - 3 provided

If anyone can assist it would be much appreciated.

0

2 Answers 2

1

Make CompareRecord a static method and change it's parameters to be const references.

class PlayerManager
{
    ...
    static bool CompareRecord(const std::vector<const char*>& record1, const std::vector<const char*>& record2);
    ...
};

You can't use non-static methods like they are reqular functions. But in your case there is no reason that CompareRecord can't be static or even just a regular function.

Sign up to request clarification or add additional context in comments.

Comments

0

Following would work:

using namespace std::placeholders;
std::sort(
    sortedHighscores.begin(),
    sortedHighscores.end(),
    std::bind(&PlayerManager::CompareRecord, this, _1, _2)
);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.