首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

使用std:sort遇到难题解决办法

2012-03-15 
使用std::sort遇到难题好的,首先,这是我的成员函数,这个成员函数可以取代oprater 重载,会读取到许多priva

使用std::sort遇到难题
好的,首先,这是我的成员函数,这个成员函数可以取代oprater> 重载,会读取到许多private变量
bool   smsOP::time_compare(const   sms_elem   &   p1,const   sms_elem   &   p2)
{
date   d1,d2;

d1   =   processTime(p1);
d2   =   processTime(p2);

if   (d1.year   >   d2.year)
return   true;
else  
if   (d1.month   >   d2.month)
return   true;
else
if   (d1.day   >   d2.day)
return   true;
else
if   (d1.hour   >   d2.hour)
return   true;
else
if   (d1.min   >   d2.min)
return   true;
else
if   (d1.sec   >   d2.sec)
return   true;
else
return   false;
}
好的,这个函数通过测试。我在另外一个函数中调用,比如,这样调用
sort(read_in.begin(),read_in.end(),time_compare);
好,立马编译报错:
j:\md\visual   studio   2005\projects\smswork\smswork\smsop.cpp(122)   :   error   C3867:   'smsOP::time_compare ':   function   call   missing   argument   list;   use   '&smsOP::time_compare '   to   create   a   pointer   to   member
j:\md\visual   studio   2005\projects\smswork\smswork\smsop.cpp(122)   :   error   C2780:   'void   std::sort(_RanIt,_RanIt) '   :   expects   2   arguments   -   3   provided
                d:\program   files\microsoft   visual   studio   8\vc\include\algorithm(3109)   :   see   declaration   of   'std::sort '

好的,改,改成这个这个样子:
sort(read_in.begin(),read_in.end(),&smsOP::time_compare);

继续报错:
d:\program   files\microsoft   visual   studio   8\vc\include\algorithm(3181)   :   error   C2064:   term   does   not   evaluate   to   a   function   taking   2   arguments
                d:\program   files\microsoft   visual   studio   8\vc\include\algorithm(3237)   :   see   reference   to   function   template   instantiation   'std::pair <_Ty1,_Ty2>   std::_Unguarded_partition <_RanIt,_Pr> (_RanIt,_RanIt,_Pr) '   being   compiled
                with
                [
                        _Ty1=std::_Vector_iterator <sms_elem,std::allocator <sms_elem> > ,
                        _Ty2=std::_Vector_iterator <sms_elem,std::allocator <sms_elem> > ,
                        _RanIt=std::_Vector_iterator <sms_elem,std::allocator <sms_elem> > ,
                        _Pr=bool   (__thiscall   smsOP::*   )(const   sms_elem   &,const   sms_elem   &)
                ]
                d:\program   files\microsoft   visual   studio   8\vc\include\algorithm(3261)   :   see   reference   to   function   template   instantiation   'void   std::_Sort <std::_Vector_iterator <_Ty,_Alloc> ,__w64   int,_Pr> (_RanIt,_RanIt,_Diff,_Pr) '   being   compiled


                with
                [
                        _Ty=sms_elem,
                        _Alloc=std::allocator <sms_elem> ,
                        _Pr=bool   (__thiscall   smsOP::*   )(const   sms_elem   &,const   sms_elem   &),
                        _RanIt=std::_Vector_iterator <sms_elem,std::allocator <sms_elem> > ,
                        _Diff=__w64   int
                ]
                j:\md\visual   studio   2005\projects\smswork\smswork\smsop.cpp(122)   :   see   reference   to   function   template   instantiation   'void   std::sort <std::_Vector_iterator <_Ty,_Alloc> ,bool(__thiscall   smsOP::*   )(const   sms_elem   &,const   sms_elem   &)> (_RanIt,_RanIt,_Pr) '   being   compiled
                with
                [
                        _Ty=sms_elem,
                        _Alloc=std::allocator <sms_elem> ,
                        _RanIt=std::_Vector_iterator <sms_elem,std::allocator <sms_elem> > ,
                        _Pr=bool   (__thiscall   smsOP::*   )(const   sms_elem   &,const   sms_elem   &)
                ]
d:\program   files\microsoft   visual   studio   8\vc\include\algorithm(3181)   :   fatal   error   C1903:   unable   to   recover   from   previous   error(s);   stopping   compilation

好了,太多了,不知道怎么解决,希望大家帮帮忙。

[解决办法]
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <sstream>
#include <functional>
using namespace std;
class Test
{
public:
Test():year(0),month(0){}
Test(const int &y,const int &m):year(y),month(m){}
Test(const pair <int,int> &ympair):\
year(ympair.first),month(ympair.second){}
inline
bool operator <(const Test &T)const
{
if(year <T.year)return true;
else if(year> T.year)return false;
else return month <T.month;
}
inline
operator string()
{
stringstream ss;
ss < <year < < "年 " < <month < < "月\n ";
return ss.str();
}
private:
int year;
int month;
};
int main()
{
vector <Test> T_vec;


T_vec.push_back(make_pair(1997,7));
T_vec.push_back(make_pair(1998,12));
T_vec.push_back(make_pair(1996,11));


T_vec.push_back(make_pair(1997,8));

copy(T_vec.begin(),T_vec.end(),ostream_iterator <string> (cout));

sort(T_vec.begin(),T_vec.end());

cout < < "after sorted... " < <endl;

copy(T_vec.begin(),T_vec.end(),ostream_iterator <string> (cout));

system( "PAUSE ");

return 0;


}

热点排行