2015-07-21 63 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);