Tom*_*hao 0 c++ sorting class function std
Entry.h:
//returns the sum of all non mega entry percentages
float sumOfNonMegaEntryPct(vector<Number>& arg1_Numbers);
Run Code Online (Sandbox Code Playgroud)
Entry.cpp:
//returns the sum of all mega entry percentages
float Entry::sumOfMegaEntryPct(vector<MegaNumber>& arg1_MegaNumbers)
{
float sumPct = 0.00f;
for (MegaNumber c : megaEntry)
{
sumPct = sumPct + arg1_MegaNumbers[c.getID()].getOccurencePct();
}
return sumPct;
}
Run Code Online (Sandbox Code Playgroud)
Lotto.h:
public:
//compares two entries, used for sorting algorithm, sorts by nonmega number
bool compareEntry_sumPct_nonMega(Entry arg1, Entry arg2);
protected:
vector<Numbers> numbers;
vector<MegaNumbers> megaNumbers;
Run Code Online (Sandbox Code Playgroud)
Lotto.cpp:
#include "lotto.h"
//sorts nonmega numbers by sum of their pct, used for sort algorithm
bool Lotto::compareEntry_sumPct_nonMega(Entry arg1, Entry arg2)
{
bool b = arg1.sumOfNonMegaEntryPct(numbers) < arg2.sumOfNonMegaEntryPct(numbers);
return b;
}
Run Code Online (Sandbox Code Playgroud)
Source.cpp:
vector<Entry> copyGameEntry = game.getPlayEntry();
sort(copyGameEntry.begin(), copyGameEntry.end(),
bind(&Lotto::compareEntry_sumPct_nonMega, game));
Run Code Online (Sandbox Code Playgroud)
这只是代码的一部分,但我认为这是有道理的.编译时,我收到错误:
严重级代码描述项目文件行错误C2451类型'std :: _ Unforced'的条件表达式是非法的彩票排序e:\ program files(x86)\ microsoft visual studio 14.0\vc\include\algorithm 3133
严重级代码说明项目文件行错误C2675一元'!':'std :: _ Unforced'未定义此运算符或转换为预定义运算符可接受的类型Lottery Sort e:\ program files(x86)\ microsoft visual studio 14.0\vc\include\algorithm 3118
题:
可能是什么问题呢 ?
您使用std::bind不当.您需要为未绑定的参数使用占位符:
using namespace std::placeholders;
sort(copyGameEntry.begin(), copyGameEntry.end(),
bind(&Lotto::compareEntry_sumPct_nonMega, game, _1, _2));
Run Code Online (Sandbox Code Playgroud)
注意,这个绑定表达式将复制该game对象,因此您应该使用std::ref(game)或仅使用它&game来避免不必要的复制.
或者使用lambda函数:
sort(copyGameEntry.begin(), copyGameEntry.end(),
[&game](Entry& l, Entry& r) {
return game.compareEntry_sumPct_nonMega(l, r);
});
Run Code Online (Sandbox Code Playgroud)