【问题标题】:sorting vector of structs by integer [duplicate]按整数排序结构向量[重复]
【发布时间】:2015-07-21 09:08:34
【问题描述】:

我正在尝试对结构向量进行排序。我看到了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 更改为&amp;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.

我很乐意为您提供帮助。

谢谢。

【问题讨论】:

  • compareByTime 必须是静态函数
  • 或者非成员函数。

标签: c++


【解决方案1】:

首先修正所有的错别字(缺少分号等)。

然后把你的排序函数改成static bool compareByTime(const commands &amp;a, const commands &amp;b);

【讨论】:

    【解决方案2】:

    简答

    制作compareByTimestatic(或“全局”类外)

    说明

    成员函数需要this 以某种方式传递给它们,所以两个参数的成员函数可能是三个参数的函数。所以编译器在需要 2 个参数比较器时不能使用它。

    【讨论】:

    • 您需要使用std::bind 来执行此操作。如果您使用的是较旧的编译器,那么您基本上是被圈套了。但是对于较新的,您现在可以了。
    猜你喜欢
    • 2011-06-21
    • 2020-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多