Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Signed/unsigned mismatch

Tags:

c++

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);
    }
}
like image 893
Madness Avatar asked Jul 22 '26 02:07

Madness


1 Answers

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 */  
}
like image 56
john.pavan Avatar answered Jul 23 '26 16:07

john.pavan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!