【问题标题】:Compilation Error using Custom Sort inside Class [duplicate]在类中使用自定义排序时出现编译错误 [重复]
【发布时间】:2019-09-02 10:35:10
【问题描述】:

我正在尝试在类中使用自定义排序, 如sort(arr,arr+n,cust); 在类函数中。 自定义排序如下所示:

bool cust (int a, int b) {
  return pos[a]<pos[b];
}

pos 是类内的数组。 但它不编译,并给出编译错误

“无效使用非静态成员函数” 如何摆脱编译错误。

我的代码 -> https://pastebin.com/W1zw0A5s

我试过写:

static bool cust (int a, int b) {
     /* same code */
} 

这没有帮助。

【问题讨论】:

  • 请在此处发布最小代码,因为 pastebin 可能会被删除。

标签: c++ class c++11


【解决方案1】:

根据 pastebin 链接,此函数是您类中的成员函数。您需要将this 指针绑定到它才能使用它:

sort(sa,sa+n, [this](int a, int b){ return this->sufCmp(a,b); } );

【讨论】:

  • 我试过了,但现在它在静态成员函数中无效使用成员 SuffixArray::pos 并且对于 gap 和 n 也是如此
  • 编辑了我的答案以避免静态成员函数。
  • 但现在它已经开始给出“成员变量的无效使用”,例如我在比较器函数中使用的 gap、pos、n。请帮忙
  • 摆脱静电...
  • @vikram 我们有一个很好的list of good C++ books 可以学习:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-14
  • 1970-01-01
  • 1970-01-01
  • 2016-12-05
  • 1970-01-01
相关资源
最近更新 更多