为什么 mingw 知道 round() 而 Visual Studio 编译器不知道

Pet*_*etr 5 c++ visual-c++

有效并由 gcc 编译但不是由 VS 编译器编译的示例代码:

#include <cmath>

int main()
{
    float x = 1233.23;
    x = round (x * 10) / 10;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

但出于某种原因,当我在 Visual Studio 中编译它时,我收到一个错误:

C3861: 'round': identifier not found
Run Code Online (Sandbox Code Playgroud)

即使cmath有人在这里建议,我也包括在内:http : //www.daniweb.com/software-development/cpp/threads/270269/boss_loken.cpp147-error-c3861-round-identifier-not-found

这个函数只在 gcc 中吗?

jua*_*nza 5

首先,cmath不能保证带入round全局命名空间,因此即使使用最新的、符合标准的 C 或 C++ 实现,您的代码也可能会失败。可以肯定的是,使用std::round(或#include <math.h>.)

请注意,你的C ++编译器必须支持C++11std::round<cmath>)。C编译器应该支持C99round(从<math.h>)。如果我提出修正以后你的MSVC版本不工作,这可能仅仅是该特定版本是预先C ++ 11,或者是根本就没有符合标准。


vit*_*rov 1

您还可以使用 boost 库:

#include <boost/math/special_functions/round.hpp>

const double a = boost::math::round(3.45); // = 3.0
const int b = boost::math::iround(3.45); // = 3
Run Code Online (Sandbox Code Playgroud)