0
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.

Adrian
  • 19,440
  • 34
  • 112
  • 219

8 Answers8

6

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.

Jeremiah Willcock
  • 30,161
  • 7
  • 76
  • 78
4

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;
    }
};
Martin York
  • 257,169
  • 86
  • 333
  • 562
  • 2
    ... because it will instantiate `std::sort` with a template argument specific to your functor type, whose `operator()` therefore can easily be inlined into that instantiation. With a function pointer, the instantiation of `std::sort` is only for the function pointer type, not for the exact function, so inlining a call to the particular function actually pointed to by the pointer is a harder job. It can't be inlined into the instantiation, only into an inlined replacement of the call to the instantiation. At least, that's what I'm told ;-) – Steve Jessop Mar 10 '11 at 20:39
  • But if the function is fixed at compile time then I see no reason that the function can not be inlined. The trouble for a compiler is proving that a function pointer is not modified at runtime, so even though we know it is not changed can the compiler prove it. – Martin York Mar 10 '11 at 23:28
  • You see no reason why the comparator cannot be inlined into (say) `std::sort`, or you see no reason why `std::sort` can't be inlined into `main`? In any case, what's relevant isn't whether it *can* be inlined (of course it can, provided that `std::sort` is inlined), it's whether it actually will be inlined, and there are more potential obstacles to inlining in the case of the function pointer. That's all, I don't think anyone is claiming that the function pointer case is *guaranteed* to be slower than the functor. – Steve Jessop Mar 10 '11 at 23:50
  • As for "can it prove it", that particular part is basically the same problem the compiler faces when it inlines a function and you've passed it (say) an integer literal as a parameter. Decent optimizers do propagate the value through the inlined code, and can for example do dead code elimination for conditionals involving the previously-variable-parameter-now-constant-value. So it's certainly not beyond the wit of a normal compiler to determine what function is actually called, provided `std::sort` is inlined. – Steve Jessop Mar 10 '11 at 23:58
1

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

Community
  • 1
  • 1
Darius Kucinskas
  • 10,193
  • 12
  • 57
  • 79
1

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

detunized
  • 15,059
  • 3
  • 48
  • 64
0

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

Timo Geusch
  • 24,095
  • 5
  • 52
  • 70
0

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

Brian Roach
  • 76,169
  • 12
  • 136
  • 161
0
#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).

tauran
  • 7,986
  • 6
  • 41
  • 48
0

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

Erik
  • 88,732
  • 13
  • 198
  • 189