2015-07-21 81 views
0

我試圖排序結構向量。我看到了thisthis的例子。按整數排序結構向量

這是我的代碼:

.h文件中:

// I didn't mention all the includes and namespace 
class fileLoader 
{ 
struct commands 
    { 
    int time; 
    string name; 
    }; 
commands resultStruct; 
vector<commands> resultVector 

    private: 
    void sortFunction(); 
    bool compareByTime(const commands &a, const commands &b); 
} 

.cpp文件:

void fileLoader::sortResults() 
{ 
    sort(resultVector.begin(), resultVector.end(), compareByTime); 
} 

bool fileLoader::compareByTime(const commands &a, const commands &b) 
{ 
    return a.time < b.time; 
} 

這是編譯錯誤,我得到:

error C3867: 'fileLoader::compareByTime': function call missing argument list; use '&fileLoader::compareByTime' to create a pointer to member 
error C2780: 'void std::sort(_RanIt,_RanIt)' : expects 2 arguments - 3 provided 
c:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm(3639) : see declaration of 'std::sort' 

當我試圖改變compareByTime&fileLoader::compareByTime,我得到這個編譯錯誤:

error C2064: term does not evaluate to a function taking 2 arguments 
    1>   c:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm(3776) : see reference to function template instantiation 'std::pair<_Ty1,_Ty2> std::_Unguarded_partition<_RanIt,_Pr>(_RanIt,_RanIt,_Pr)' being compiled 
    1>   with 
    1>   [ 
    1>    _Ty1=fileLoader::commands *, 
    1>    _Ty2=fileLoader::commands *, 
    1>    _RanIt=fileLoader::commands *, 
    1>    _Pr=bool (__thiscall fileLoader::*)(const fileLoader::commands &,const fileLoader::commands &) 
    1>   ] 
    1>   c:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm(3806) : see reference to function template instantiation 'void std::_Sort<fileLoader::commands*,__w64 int,_Pr>(_RanIt,_RanIt,_Diff,_Pr)' being compiled 
    1>   with 
    1>   [ 
    1>    _Pr=bool (__thiscall fileLoader::*)(const fileLoader::commands &,const fileLoader::commands &), 
    1>    _RanIt=fileLoader::commands *, 
    1>    _Diff=__w64 int 
    1>   ] 
    std::sort<std::_Vector_iterator<_Myvec>,bool(__thiscall fileLoader::*)(const fileLoader::commands &,const fileLoader::commands &)>(_RanIt,_RanIt,_Pr)' being compiled 
    1>   with 
    1>   [ 
    1>    _Myvec=std::_Vector_val<fileLoader::commands,std::allocator<fileLoader::commands>>, 
    1>    _RanIt=std::_Vector_iterator<std::_Vector_val<fileLoader::commands,std::allocator<fileLoader::commands>>>, 
    1>    _Pr=bool (__thiscall fileLoader::*)(const fileLoader::commands &,const fileLoader::commands &) 
    1>   ] 
    1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm(3720): fatal error C1903: unable to recover from previous error(s); stopping compilation 
    1> 
    1>Build FAILED. 

我會很樂意爲一些幫助。

謝謝。

+0

compareByTime必須是靜態函數 – Vinzenz

+0

還是非成員函數。 – juanchopanza

回答

0

簡短的回答

compareByTimestatic(或 '全球性' 外類)

說明

會員功能需要this以某種方式傳遞給他們,於是兩個參數的成員函數大概是3個參數函數。所以編譯器在需要2個參數比較器時不能使用它。

+0

你需要使用'std :: bind'來做到這一點。如果你使用的是較老的編譯器,那麼基本上就是這樣。但對於新的,你現在沒問題。 –

0

首先修復所有錯別字(缺少分號等)。

然後改變你的排序功能static bool compareByTime(const commands &a, const commands &b);