我在我的Qt C++应用程序中有这段代码。我用于比较结构的operator==总是返回false,即使它们是相等的。我的代码出了什么问题?
下面是一个有问题的代码片段:
struct pSettings
{
int speciality;
bool autoCompleteByWord;
bool showChronicConditions;
bool showNavArrows;
bool smallScreenMode;
bool simpleExamination;
bool alwaysSave;
bool inLinePatientList;
double newVisitPrice;
double followVisitprice1;
double followVisitprice2;
double followVisitprice3;
double followVisitprice4;
int autosaveinterval;
bool autoSave ;
bool minimizeToTray;
int selectedTheme;
QString textColor;
QString topBGColor;
QString bottomBGColor;
QString altWinColor;
QString buttonColor;
QString altButtonColor;
QString textBGColor;
QString borderColor1;
QString borderColor2;
QString altButtonColorHover;
QString buttonColorHover;
QString buttonColorDisabled;
QString buttonBorderColorHover;
QString comboBGcolor;
QString buttonTextColor;
QString comboTextColor;
double lOffSet,tOffSet;
QString defaultFont;
double defaultFontSize;
bool defaultFontBold;
QString textboxFont;
double textboxFontSize;
bool textboxFontBold;
int maxFollowUps;
bool autoClosePrintDlg;
int maxFollowUpsPerProblem;
bool autoSetnewAfterMaxPerProblemIsReached;
int checkoutDate;
int checkoutTime;
bool enableVisualEffects;
bool operator==(const pSettings& psettings) const
{
return std::tie(
speciality,
autoCompleteByWord,
showChronicConditions,
showNavArrows,
smallScreenMode,
simpleExamination,
alwaysSave,
inLinePatientList,
newVisitPrice,
followVisitprice1,
followVisitprice2,
followVisitprice3,
followVisitprice4,
autosaveinterval,
autoSave ,
minimizeToTray,
selectedTheme,
textColor,
topBGColor,
bottomBGColor,
altWinColor,
buttonColor,
altButtonColor,
textBGColor,
borderColor1,
borderColor2,
altButtonColorHover,
buttonColorHover,
buttonColorDisabled,
buttonBorderColorHover,
comboBGcolor,
buttonTextColor,
comboTextColor,
lOffSet,
tOffSet,
defaultFont,
defaultFontSize,
defaultFontBold,
textboxFont,
textboxFontSize,
textboxFontBold,
maxFollowUps,
autoClosePrintDlg,
maxFollowUpsPerProblem,
autoSetnewAfterMaxPerProblemIsReached,
checkoutDate,
checkoutTime,
enableVisualEffects) ==
std::tie(psettings.speciality,
psettings.autoCompleteByWord,
psettings.showChronicConditions,
psettings.showNavArrows,
psettings.smallScreenMode,
psettings.simpleExamination,
psettings.alwaysSave,
psettings.inLinePatientList,
psettings.newVisitPrice,
psettings.followVisitprice1,
psettings.followVisitprice2,
psettings.followVisitprice3,
psettings.followVisitprice4,
psettings.autosaveinterval,
psettings.autoSave ,
psettings.minimizeToTray,
psettings.selectedTheme,
psettings.textColor,
psettings.topBGColor,
psettings.bottomBGColor,
psettings.altWinColor,
psettings.buttonColor,
psettings.altButtonColor,
psettings.textBGColor,
psettings.borderColor1,
psettings.borderColor2,
psettings.altButtonColorHover,
psettings.buttonColorHover,
psettings.buttonColorDisabled,
psettings.buttonBorderColorHover,
psettings.comboBGcolor,
psettings.buttonTextColor,
psettings.comboTextColor,
psettings.lOffSet,
psettings.tOffSet,
psettings.defaultFont,
psettings.defaultFontSize,
psettings.defaultFontBold,
psettings.textboxFont,
psettings.textboxFontSize,
psettings.textboxFontBold,
psettings.maxFollowUps,
psettings.autoClosePrintDlg,
psettings.maxFollowUpsPerProblem,
psettings.autoSetnewAfterMaxPerProblemIsReached,
psettings.checkoutDate,
psettings.checkoutTime,
psettings.enableVisualEffects);
}
};发布于 2016-10-12 17:55:17
在teo长列表中的某处,您会发现它们不完全相同的错误。你违反了DRY (不要重复)。
在C++14中:
auto mytie() const {
return std::tie( /* ... fields */ );
}然后使用它来编写==。
在C++11中,您必须将mytie(blah const& self)作为本地(无状态) lambda来编写,以保持合理。
如果这失败了,那么它们会比较不相等,因为它们的不同之处是你没有注意到的。
发布于 2018-12-24 19:07:50
为此,您可以使用高阶宏函数:
#define ITEMS \
ITEM(int, speciality) \
ITEM(bool, autoCompleteByWord) \
...
ITEM(int, checkoutTime) \
LAST(bool, enableVisualEffects)
#define ITEM(x,y) x y;
#define LAST(x,y) ITEM(x,y)
struct pSettings
{
ITEMS
}
#define ITEM(x,y) y,
#define LAST(x,y) y
auto mytie() const
{
return std::tie
(
ITEMS
);
}最好在单独的文件中编写项定义
https://stackoverflow.com/questions/39995458
复制相似问题