简体   繁体   中英

C++ Signed/unsigned mismatch

I am having trouble getting some code to compile properly. Here are my errors:

warning C4018: '>=' : signed/unsigned mismatch

void Player::HasteCap()
{
    if (sWorld->getBoolConfig(CONFIG_PLAYER_HASTECAP_ENABLE))
        return;

    bool hasInstantHasteCap = (GetFloatValue(UNIT_FIELD_BASEATTACKTIME + 0) == 1 
                            || GetFloatValue(UNIT_FIELD_BASEATTACKTIME + 1) == 1 
                            || GetFloatValue(UNIT_FIELD_BASEATTACKTIME + 2) == 1 
                            || GetFloatValue(UNIT_MOD_CAST_SPEED) == 0);

    if (m_baseRatingValue[CR_HASTE_MELEE] > sWorld->getIntConfig(CONFIG_PLAYER_HASTECAP_LIMIT))
    {
        SetFloatValue(UNIT_MOD_CAST_SPEED, 0);
        SetFloatValue(UNIT_FIELD_BASEATTACKTIME + BASE_ATTACK, 1);
        SetFloatValue(UNIT_FIELD_BASEATTACKTIME + OFF_ATTACK, 1);
        SetFloatValue(UNIT_FIELD_BASEATTACKTIME + RANGED_ATTACK, 1);
    }

    else if (hasInstantHasteCap && m_baseRatingValue[CR_HASTE_MELEE] < sWorld->getIntConfig(CONFIG_PLAYER_HASTECAP_LIMIT))
    {
        SetFloatValue(UNIT_MOD_CAST_SPEED, 1.0f);
        SetRegularAttackTime();
        ApplyCastTimePercentMod(m_baseRatingValue[CR_HASTE_SPELL] * GetRatingMultiplier(CR_HASTE_SPELL), true);

        if (GetShapeshiftForm())
        {
            SpellShapeshiftEntry const* ssEntry = sSpellShapeshiftStore.LookupEntry(GetShapeshiftForm());
            if (ssEntry && ssEntry->attackSpeed)
            {
                SetAttackTime(BASE_ATTACK, ssEntry->attackSpeed);
                SetAttackTime(OFF_ATTACK, ssEntry->attackSpeed);
                SetAttackTime(RANGED_ATTACK, BASE_ATTACK_TIME);
            }
        }
    }

    if (CanModifyStats())
    {
        UpdateDamagePhysical(BASE_ATTACK);
        UpdateDamagePhysical(OFF_ATTACK);
        UpdateDamagePhysical(RANGED_ATTACK);
    }
}

The signed/unsigned nature of the two values you are comparing should be the same, otherwise one gets cast as the other for the comparison, which can lead to unexpected results.

It would be best to make sure that what you're comparing are the same type, but:

If you know which value is safe to cast, explicitly cast that one. In your case, case the signed value as an unsigned value.

eg

unsigned int val1 = someunsignedvalue;  
int val2 = somesignedvalue;    
if (val1 > (unsigned int) val2) {
    /* do stuff */  
}

These are warnings not errors. Normally, the code should compile in spite of warnings. If you, however, specify the "treat warnings as errors" compiler option, the compiler will not compile if it produces any warnings (or errors).

To work around that warning, you can cast one of the unsigned side of the logical operator to int.

The code you posted did not contain the definitions for the members/functions in question, but the compiler is probably correct.

What kind of answer do you expect?

either fix the problem (change the members/functions/do a C style cast/do a static_cast<...> etc so the "signedness" of both sides of the comparison match), or disable this warning with the suitable #pragma

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM