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

C++语言 码拜 9年前 (2016-04-24) 6021次浏览
找了一下午还是没搞懂,为什么会这样报错,错误:reference to non-static member function must be call

#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);
                    }
           }
        }
};

本人把这些函数不写在类中,而是作普通函数,就不会报错,求高手啊

解决方案

80

成员函数第一个参数默认是this指针, 所以compare_times函数其实是3个参数
加static  ,静态函数就没有this指针,但你又调用了times成员函数。本人服了
题主还是把compare_times写到类外面去吧

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明错误:reference to non-static member function must be call
喜欢 (1)
[1034331897@qq.com]
分享 (0)