2015-07-21 2 views
0

Im пытается сортировать вектор структур. Я видел this и this примеров.сортировка вектор structs целым числом

это мой код:

.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);

Смежные вопросы