小编kos*_*sei的帖子

C++ std :: sort with Class中的谓词函数

我想在某个类中按某种顺序对某个结构的向量进行排序.我在类中编写了struct和predicate函数的定义,并在具有这些struct和function的类的方法中运行std :: sort.但是发生了编译错误.gcc版本是4.0.1,OS是Mac OSX.代码如下:

class sample {
public:
  struct s {
    int x;
    int y;
  };

  bool cmp (struct s a, struct s b) {
    if (a.x == b.x)
      return a.y < b.y;
    else
      return a.x < b.x;
  }

  int func(void) {
    std::vector <struct s> vec;

    // ...

    sort(vec.begin(), vec.end(), cmp);  // compilation error

    // ...

    return 0;
  }
};

int main(void) {
  sample *smp = new sample();
  smp->func();
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

错误消息庞大而复杂.所以这是前两行.

sortSample.cpp:在成员函数'INT ::样品FUNC()':
sortSample.cpp:51:错误:类型的参数'布尔(样品::)(样品:: S,样品:: S)'不匹配'bool(sample ::*)(sample :: …

c++

4
推荐指数
3
解决办法
1万
查看次数

标签 统计

c++ ×1