1

我在 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);
    }

};
4

2 回答 2

5

在 teo 长列表中的某处,您在它们不相同的地方遇到错误。你违反了 DRY(不要重复自己)。

在 C++14 中:

auto mytie() const {
  return std::tie( /* ... fields */ );
}

然后==用那个写。

在 C++11 中,您必须编写mytie(blah const& self)为本地(无状态)lambda 以保持合理。

如果这失败了,那么它们比较不相等,因为它们以您没有注意到的方式不同。

于 2016-10-12T09:55:16.913 回答
0

您可以为此使用高阶宏函数:

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

在单独的文件中编写ITEMS定义是个好主意

于 2018-12-24T11:07:49.867 回答