Ambiguous overload call to abs(double)

后端 未结 4 1098
孤街浪徒
孤街浪徒 2020-12-23 11:06

I have the following C++ code:

#include 
#include       // per http://www.cplusplus.com/reference/clibrary/cmath/abs/

// snip .         


        
4条回答
  •  长情又很酷
    2020-12-23 11:59

    Its boils down to this: math.h is from C and was created over 10 years ago. In math.h, due to its primitive nature, the abs() function is "essentially" just for integer types and if you wanted to get the absolute value of a double, you had to use fabs(). When C++ was created it took math.h and made it cmath. cmath is essentially math.h but improved for C++. It improved things like having to distinguish between fabs() and abs, and just made abs() for both doubles and integer types. In summary either: Use math.h and use abs() for integers, fabs() for doubles or use cmath and just have abs for everything (easier and recommended)

    Hope this helps anyone who is having the same problem!

提交回复
热议问题