C++ – Why round() is known by mingw but not by visual studio compiler

cvisual c++

Sample code which is valid and gets compiled by gcc but not by VS compiler:

#include <cmath>

int main()
{
    float x = 1233.23;
    x = round (x * 10) / 10;
    return 0;
}

but for some reason, when I am compiling this in Visual Studio I get an error:

C3861: 'round': identifier not found

I do include even cmath as someone suggested here: http://www.daniweb.com/software-development/cpp/threads/270269/boss_loken.cpp147-error-c3861-round-identifier-not-found

Is this function in gcc only?

Best Answer

First of all, cmath is not guaranteed to bring round into the global namespace, so your code could fail, even with an up-to-date, standards compliant C or C++ implementation. To be sure, use std::round (or #include <math.h>.)

Note that your C++ compiler must support C++11 for std::round (<cmath>). A C compiler should support C99 for round (from <math.h>.) If your version of MSVC doesn't work after the fix I suggested, it could simply be that that particular version is pre-C++11, or is simply not standards compliant.

Related Topic