Madness
Madness

Reputation: 638

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

Upvotes: 3

Views: 19013

Answers (3)

john.pavan
john.pavan

Reputation: 950

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.

e.g.

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

Upvotes: 7

mefathy
mefathy

Reputation: 781

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.

Upvotes: 3

yonil
yonil

Reputation: 574

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

Upvotes: 1

Related Questions