2011-03-10 168 views
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&)] 

爲什麼這不正確,我的編譯器給了我那個錯誤。
而我希望我的功能是全球性的。
謝謝。對C++中的對象進行排序

回答

6

sortingVector某個類的非靜態成員?該錯誤消息表明,在這種情況下,您需要將其包裝(例如,使用boost::bind)到二進制操作中,該操作不需要參數this。您可能想將sortingVector改爲靜態成員或免費函數。

+0

是的,這是問題,我didint知道這一點。下次我會記住這一點,謝謝 – Kobe 2011-03-10 20:15:13

0

你可能想打電話std::sort而不是純粹的排序,可能必須包括適當的頭文件(algorithm,除非我錯了)。

+0

是的,我包括,看起來像我必須使該功能靜態 – Kobe 2011-03-10 20:16:01

1

我相信這裏正確的榜樣 - >Sorting a vector of custom objects

+0

坦克的鏈接 – Kobe 2011-03-10 20:15:33

+0

是的,但使用函數進行比較也很好。 – tauran 2011-03-10 20:17:08

+0

@tauran - 是的,你是對的 – 2011-03-10 20:19:23

0

using namespace std;?如果不是你想要std::sort()

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; 
} 

編譯我的機器上(GCC 4.4.3版)的罰款。

4

這是一個編譯例子應該是什麼樣子:

因爲你沒有提供這是產生人都給予了幾種不同類型的答案的錯誤確切的代碼。因此,生成一個可編譯的例子顯示問題通常是一個好主意。

#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); 
} 

一般的編譯器可以做優化的更好的工作(據我所知)如果你使用一個仿函數,而不是一個函數指針。

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

...因爲它會使用特定於你的函子類型的模板參數來實例化'std :: sort',它的'operator()'因此可以很容易地被內聯到那個實例中。使用函數指針,std :: sort'的實例化僅用於函數指針類型,而不是用於確切的函數,因此內聯調用指針實際指向的特定函數是一件比較困難的工作。它不能被內聯到實例化中,只能被內聯替換爲實例化的調用。至少,這就是我所知道的;-) – 2011-03-10 20:39:46

+0

但是,如果函數在編譯時是固定的,那麼我看不出該函數不能被內聯的原因。編譯器的麻煩是證明函數指針在運行時沒有被修改,所以即使我們知道它沒有改變,編譯器也能證明它。 – 2011-03-10 23:28:58

+0

你看不出爲什麼比較器不能內聯到(比如說)'std :: sort ',或者你看不到爲什麼'std ::排序'不能被內聯到'main'?在任何情況下,相關的不是它是否可以被內聯(當然,它可以,只要std :: sort被內聯),它是否真的被內聯,並且內聯有更多的潛在障礙在函數指針的情況下。就這樣,我不認爲有人聲稱函數指針的情況下*保證*比仿函數慢。 – 2011-03-10 23:50:14

1

std::放在你的sort的電話前面。和源文件頂部的#include <algorithm>

0

您有多個排序向量函數 - 因此您的錯誤的<unresolved overloaded function type>部分。