错误:reference to non-static member function must be called

Bovinitwo 2016-05-30 05:28:26
找了一下午还是没搞懂,为什么会这样报错,
#include <algorithm>
class Coder {
public:
vector<string> findCoder(vector<string> &A, int n)
{
vector<string> result;

stable_sort(A.begin(),A.end(),compare_times);//报错:reference to non-static member function must be called

vector<string>::iterator p_Push=find_first(A);
while(p_Push<A.end())
{
result.push_back(*p_Push);
p_Push++;
}
return result;
}

bool compare_times(const string &a,const string &b)
{
return times(a)<times(b);
}

vector<string>::iterator find_first(vector<string> &A)
{
for(vector<string>::iterator pFirst=A.begin();pFirst!=A.end();pFirst++)
{
if(times(*pFirst)!=0)
return pFirst;
}
}

int times(string a,int currentIndex=0,int state=0)
{
if(currentIndex==a.size())
return 0;
switch (state)
{
case 0:
{
if(a[currentIndex]=='c'||a[currentIndex]=='C') return times(a,currentIndex+1,1);
else return times(a,currentIndex+1,0);
}
case 1:
{
if(a[currentIndex]=='o'||a[currentIndex]=='O') return times(a,currentIndex+1,2);
else return times(a,currentIndex+1,0);
}
case 2:
{
if(a[currentIndex]=='d'||a[currentIndex]=='D') return times(a,currentIndex+1,3);
else return times(a,currentIndex+1,0);
}
case 3:
{
if(a[currentIndex]=='e'||a[currentIndex]=='E') return times(a,currentIndex+1,4);
else return times(a,currentIndex+1,0);
}
default:
{
if(a[currentIndex]=='r'||a[currentIndex]=='R') return 1+times(a,currentIndex+1,0);
else return times(a,currentIndex+1,0);
}
}
}
};


我把这些函数不写在类中,而是作普通函数,就不会报错,求大神啊
...全文
3622 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Bovinitwo 2016-05-30
  • 打赏
  • 举报
回复
引用 5 楼 qq423399099 的回复:
成员函数第一个参数默认是this指针, 所以compare_times函数其实是3个参数 加static ,静态函数就没有this指针,但你又调用了times成员函数。。。我服了 楼主还是把compare_times写到类外面去吧
谢谢,我下来再看看书
小灸舞 2016-05-30
  • 打赏
  • 举报
回复
成员函数第一个参数默认是this指针, 所以compare_times函数其实是3个参数
加static ,静态函数就没有this指针,但你又调用了times成员函数。。。我服了
楼主还是把compare_times写到类外面去吧
Bovinitwo 2016-05-30
  • 打赏
  • 举报
回复
引用 1 楼 dustpg 的回复:
面向对象编程不是逮着就加个class
这是牛客网的OJ,不是我要写一个class,然后一直不能通过编译,搜了一下网上的相关答案也不是很明白,所以上来问问
Bovinitwo 2016-05-30
  • 打赏
  • 举报
回复
找了一下午还是没搞懂,为什么会这样报错, 重新贴一下代码: class Coder { public: vector<string> findCoder(vector<string> &A, int n) { vector<string> result; stable_sort(A.begin(),A.end(),compare_times); //报错:reference to non-static member function must be called vector<string>::iterator p_Push=find_first(A); while(p_Push<A.end()) { result.push_back(*p_Push); p_Push++; } return result; } bool compare_times(const string &a,const string &b) { return times(a)<times(b); } vector<string>::iterator find_first(vector<string> &A) { for(vector<string>::iterator pFirst=A.begin();pFirst!=A.end();pFirst++) { if(times(*pFirst)!=0) return pFirst; } } int times(string a,int currentIndex=0,int state=0) { if(currentIndex==a.size()) return 0; switch (state) { case 0: { if(a[currentIndex]=='c'||a[currentIndex]=='C') return times(a,currentIndex+1,1); else return times(a,currentIndex+1,0); } case 1: { if(a[currentIndex]=='o'||a[currentIndex]=='O') return times(a,currentIndex+1,2); else return times(a,currentIndex+1,0); } case 2: { if(a[currentIndex]=='d'||a[currentIndex]=='D') return times(a,currentIndex+1,3); else return times(a,currentIndex+1,0); } case 3: { if(a[currentIndex]=='e'||a[currentIndex]=='E') return times(a,currentIndex+1,4); else return times(a,currentIndex+1,0); } default: { if(a[currentIndex]=='r'||a[currentIndex]=='R') return 1+times(a,currentIndex+1,0); else return times(a,currentIndex+1,0); } } } };
dustpg 2016-05-30
  • 打赏
  • 举报
回复
面向对象编程不是逮着就加个class

65,212

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧