M_PI flagged as undeclared identifier

假装没事ソ 提交于 2019-11-30 02:59:58

It sounds like you're using MS stuff, according to their docs

Math Constants are not defined in Standard C/C++. To use them, you must first define _USE_MATH_DEFINES and then include cmath or math.h.

So you need something like

#define _USE_MATH_DEFINES
#include <cmath>

as a header.

math.h not defines M_PI by default. So go with this:

#ifndef M_PI
    #define M_PI 3.14159265358979323846
#endif

This will handle both cases either your header have M_PI defined or not.

M_PI is supported by GCC too, but you've to do some work to get it

#undef __STRICT_ANSI__
#include <cmath>

or if you don't like to pollute your source file, then do

g++ -U__STRICT_ANSI__ <other options>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!