3

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);
    }
}
KindDragon
  • 6,558
  • 4
  • 47
  • 75
Madness
  • 618
  • 2
  • 7
  • 22
  • 1
    Duplicate of http://stackoverflow.com/questions/7443222/how-do-i-deal-with-signed-unsigned-mismatch-warnings-c4018?rq=1 ? – Slava Feb 25 '13 at 01:21
  • Apologies, I am learning C++ and understand the problem (Unassigned int) but really can't figure out how to fix it in this instance. – Madness Feb 25 '13 at 01:23
  • Unsigned int not unassigned, it is not problem that you are asking questions, problem that you did not check if it was answered before – Slava Feb 25 '13 at 01:27

3 Answers3

7

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 */  
}
KindDragon
  • 6,558
  • 4
  • 47
  • 75
john.pavan
  • 910
  • 4
  • 6
  • 1
    where "safe" means `val2` is >= 0. – Alexey Frunze Feb 25 '13 at 01:33
  • Thank you, this helped a lot. Apologies to everyone that this has been posted before, but I just had trouble understanding signing of int's with the others. – Madness Feb 25 '13 at 01:33
  • I don't think you've shared quite enough code for us to tell you. What are the return types for: sWorld->getIntConfig(CONFIG_PLAYER_HASTECAP_LIMIT) and what type is: m_baseRatingValue[CR_HASTE_MELEE] – john.pavan Feb 25 '13 at 02:02
  • This is not in order but here's most of it: http://pastebin.com/n9w6MAyu CR_HASTE_MELEE is an enum and sWorld writes to a config file, where a value can be given to modify a players attack speed. – Madness Feb 25 '13 at 02:31
  • Ok, I don't think what you posted on pastebin actually had the information I asked for, so I'm going to guess. I believe that sWorld's getIntConfig function returns an int (signed) and m_baseRatingValue is an unsigned type. They should be the same type (look at their declarations, which I did not see in your post). – john.pavan Feb 25 '13 at 02:54
  • Oops I'm sorry. This system is so huge it's difficult finding everything. I've just found both and they do use mismatched int signings! I'll change them and see how it goes. Thanks so much for your help. – Madness Feb 25 '13 at 07:55
3

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.

mefathy
  • 771
  • 5
  • 10
1

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

yonil
  • 564
  • 3
  • 12