Adrian
Adrian

Reputation: 20068

Sorting a vector of objects in C++

struct Keyword
{
    std::string keyword;
    int numUses;
};  

bool sortingVector(const Keyword& key1, const Keyword& key2)
{
    return key1.numUses < key2.numUses;
}

sort(topKeywords.begin(), topKeywords.end(), sortingVector);



: no matching function for call to 'sort(std::vector<Keyword>::iterator, std::vector<Keyword>::iterator, <unresolved overloaded function type>)'
        c:\mingw\bin\../lib/gcc/mingw32/4.5.0/include/c++/bits/stl_algo.h:5236:18: note: candidate is: void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator<Keyword*, std::vector<Keyword> >, _Compare = bool (NewsAggregatorImpl::*)(const Keyword&, const Keyword&)]

Why isn't this correct, my compiler gives me that error.
And i want my function to be global.
Thank you.

Upvotes: 0

Views: 8633

Answers (8)

Erik
Erik

Reputation: 91270

You have multiple sortingVector functions - thus the <unresolved overloaded function type> part of your error.

Upvotes: 0

detunized
detunized

Reputation: 15289

Put std:: in front of your sort call. And #include <algorithm> at the top of the source file.

Upvotes: 1

Loki Astari
Loki Astari

Reputation: 264381

This is what a compilable example should look like:

Because you did not provide the exact code that was generating the errors people have given a couple of different types of answer. As a result it is generally a good idea to generate a compilable example the displays the problem.

#include <algorithm>
#include <vector>
#include <string>

struct Keyword
{
        std::string keyword;
            int numUses;
};

bool sortingVector(const Keyword& key1, const Keyword& key2)
{
        return key1.numUses < key2.numUses;
}

int main()
{
    std::vector<Keyword>    topKeywords;

    std::sort(topKeywords.begin(), topKeywords.end(), sortingVector);
}

Generally the compiler can do a better job of optimising (I am told) if you use a functor rather than a function pointer.

struct SortingVectorFunctor
{
    bool operator()(const Keyword& key1, const Keyword& key2) const
    {
        return key1.numUses < key2.numUses;
    }
};

Upvotes: 4

tauran
tauran

Reputation: 8036

#include <vector>
#include <algorithm>
#include <string>

struct Keyword
{
    std::string keyword;
    int numUses;
};  

bool sortingVector(const Keyword& key1, const Keyword& key2)
{
    return key1.numUses < key2.numUses;
}

int main()
{
    std::vector<Keyword> topKeywords(100);

    // imagine topKeywords initialization here

    std::sort(topKeywords.begin(), topKeywords.end(), sortingVector);

    return 0;
}

Compiles fine on my machine (gcc version 4.4.3).

Upvotes: 0

Brian Roach
Brian Roach

Reputation: 76898

are you using namespace std; ? if not you want std::sort()

Upvotes: 0

Darius Kucinskas
Darius Kucinskas

Reputation: 10661

I believe right example here -> Sorting a vector of custom objects

Upvotes: 1

Jeremiah Willcock
Jeremiah Willcock

Reputation: 30969

Is sortingVector a non-static member of some class? The error message suggests that it is, in which case you will need to wrap it (using boost::bind, for example) into a binary operation that doesn't take a this parameter. You might want to make sortingVector a static member or a free function instead.

Upvotes: 6

Timo Geusch
Timo Geusch

Reputation: 24341

You probably want to call std::sort instead of plain sort and might have to include the appropriate header file (algorithmunless I'm mistaken).

Upvotes: 0

Related Questions