我包含<cmath>但无法编译标准::abs(double)



重新安装Rad Studio XE2后,我发现一些用于编译的代码不再工作。例如,我在以下内容上得到一个编译器错误:

#include <cmath>
void MyClass::Rotate(double RotAngle){
  bool NotRotated = std::abs(RotAngle) < 1;
  ... do something
}

出现以下错误:

[BCC32 Error] XXX.cpp(38): E2015 Ambiguity between 'std::abs(int) at c:program files (x86)embarcaderorad studio9.0includewindowscrtlstdlib.h:142' and 'std::abs(__int64) at c:program files (x86)embarcaderorad studio9.0includewindowscrtlstdlib.h:538'

这段代码是用来编译的,显然应该编译,那么我缺少什么呢?Rad_studio已应用所有更新。。

在这种情况下,我们实际上可以从标准中学到很多东西。

C++98:在26.5/表80和81中,我们了解到abs<cstdlib>中,而不是在<cmath>中。但是在26.5中,我们有一个矛盾的语句In addition to the double versions of the math functions in <cmath>, C++ adds float and long double overloaded versions of these functions, with the same semantics.,它将abs列为在<cmath>中有额外的过载,而上一个表说它不应该是

这在C++11中实际上是固定的,26.8/表119清楚地显示abs<cmath>的成员,以及<cstdlib>中的(尽管为浮点类型添加的重载似乎仍然是<cmath>独有的。

至于你的问题,有两种可能的情况:

  1. 您以前是以C++11编译的,现在不再这样做了
  2. 您正在编译C++98/03,但您的标准库已更新到<cmath>不再隐式包含<cstdlib>的版本。您的编译器基于表要求,而不是26.5的隐式要求

包含<cstdlib>很可能会像在C++11模式下编译一样解决问题。

最新更新