在MFC中使用sort函数做排序时遇到一个小bug 记录一下
在MFC中使用sort函数出错
class CClientSocket :
public CSocket
{
public:
CClientSocket(void);
~CClientSocket(void);
virtual void OnReceive(int nErrorCode); //重写接收函数,通过类向导生成
BOOL SendMSG(LPSTR lpBuff,int nlen); //发送函数,用于发送数据给服务器
BOOL highPricerank(const StockRank &a,const StockRank &b);
BOOL closePricerank(const StockRank &a,const StockRank &b);
BOOL lowPricerank(const StockRank &a,const StockRank &b);
BOOL openPricerank(const StockRank &a,const StockRank &b);
};
在该头文件中做了声明,相应的cpp文件中实现函数。编译时报错,stable_sort(last,last+50,&CClientSocket::highPricerank)出了错误。
提示错误代码为:error C2064: 项不会计算为接受 2 个参数的函数。
错误原因:操作func函数要是全局函数或者类的静态函数,不能定义为类的静态函数。
在相应cpp文件外部定义需要的func函数为全局函数,将类中的成员函数注释掉。
using namespace std;
CClientSocket::CClientSocket(void)
{
}
CClientSocket::~CClientSocket(void)
{
}
BOOL openPricerank(const StockRank &a,const StockRank &b)
{
return a.openPriceRank<b.openPriceRank;
}
在后面使用stable_sort函数,stable_sort(now,now+50,openPricerank);即可,编译通过