floor()

Jul 14, 2011 at 2:11pm
Hi everybody,

Few moments ago I have stopped using DevCpp and started with VC++ 2005.
I had this line:

 
cout<<" "<<Player.EN()+round(Player.ENDUR()/10);


In devcpp it was ok but in VC not.
I found out that MSVC doesnt have round() and I have to change it to floor() so I do it like this:

 
cout<<" "<<Player.EN()+floor(Player.ENDUR()/10);


But now it is cousing this error

.\AGADG.cpp(75) : error C2668: 'floor' : ambiguous call to overloaded function


I dont know why it is writting that ENDUR() is overloaded function 'cause I have it only once in my program.
This is part of class for object Player

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Hrac{
      private:
              int hp, en, exp, lvl, mindm, maxdm;
              int stam, endur, stren, spell[10], spellCD[10];
              string nick, pass;
      public:
             Hrac(){
                    hp=100; en=100, exp=0, lvl=1, mindm=5, maxdm=10;
                    stam=0; endur=0; stren=0; spell[0]=1; spell[1]=2;
                    spellCD[0]=25; spellCD[1]=70;
                    for(int i=2;i<=9; i++) spell[i]=0;
                    }
             .
             .
             .
             .
             int HP(){ return hp; }
             int EN(){ return en; }
             int EXP(){ return exp; }
             int LVL(){ return lvl; }
             int MINDM(){ return mindm; }
             int MAXDM(){ return maxdm; }
             int STAM(){ return stam;  }
             int ENDUR(){ return endur;  }
             int STREN(){ return stren; }
             int SPELL(int i){ return spell[i]; }
             int SpellCD(int i){ return spellCD[i]; }
             void SetNick(string NNick){ nick=NNick; }
             void SetPass(string Npass){ pass= Npass; }
};



Can someone explain me please, why it is causing that error??
Jul 14, 2011 at 2:34pm
The error is there because the CMATH header has three variations of floor.
One is a double, another float and the last long double.
ENDUR is of type int so it can't choose which function to choose.

You should cast the value in either double, float or long double.

Here is a useful link about floor():
http://www.cplusplus.com/reference/clibrary/cmath/floor/
Jul 14, 2011 at 2:34pm
See here:
http://www.cplusplus.com/reference/clibrary/cmath/floor/

There are three "official" variants of floor and none of them takes an int as a parameter.
Since int can be implicitly converted to all three types, it's an ambiguous call.
Divide by 10.0 to make the result a double.

Edit: oh, too slow.
Last edited on Jul 14, 2011 at 2:35pm
Jul 14, 2011 at 2:42pm
Please check the documentation for available floor function. There must be more than one versions where the function takes different data types. i.e. it's overloaded. (say float and double). Now when the compiler is trying to resolve return type of "Player.ENDUR()/10" to one of those --> perhaps converting an int to either float or double. Here it is confused, whether to convert it to float or whether to convert it to double. Hence this error. This might answer your why? You can avoid the situation by forcefully typecasting the return type of "Player.ENDUR()/10" to either a float or double and try.
Jul 15, 2011 at 7:20am
thank you very much, both three :D, it really helped me :)
Jul 15, 2011 at 7:43am
lol.
As everyone stated integer division returns an integer. So you don't need the call to floor. (well the behaviour is not actually the same, ¿but can the endurance be negative?)

Also, all those getters are ridiculous.
Jul 16, 2011 at 6:06pm
If I didnt call floor it would cause warnings at compilation and I dont want them :)
Jul 16, 2011 at 8:38pm
Sometimes you just have to go with your gut and ignore compiler warnings you know are wrong. ;P
Jul 17, 2011 at 6:54am
:D
Topic archived. No new replies allowed.