M_PI被标记为未声明的标识符 [英] M_PI flagged as undeclared identifier

查看:1108
本文介绍了M_PI被标记为未声明的标识符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我编译下面的代码时,我收到以下错误消息:

When I compile the code below, I got these error messages:

(Error  1   error C2065: 'M_PI' : undeclared identifier 
2   IntelliSense: identifier "M_PI" is undefined)

这是什么?

#include <iostream>
#include <math.h>

using namespace std;

double my_sqrt1( double n );`enter code here`

int main() {
double k[5] = {-100, -10, -1, 10, 100};
int i;

for ( i = 0; i < 5; i++ ) {
    double val = M_PI * pow( 10.0, k[i] );
    cout << "n: "
         << val
         << "\tmysqrt: "
         << my_sqrt1(val)
         << "\tsqrt: "
         << sqrt(val)
         << endl;
}

return 0;
}

double my_sqrt1( double n ) {
int i;
double x = 1;


for ( i = 0; i < 10; i++ ) {
    x = ( x + n / x ) / 2;
}

return x;
}


推荐答案

听起来像你在根据他们的文档


标准C / C ++中未定义数学常数。要使用它们,必须先定义_USE_MATH_DEFINES,然后包括cmath或math.h。

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.

因此您需要类似

#define _USE_MATH_DEFINES
#include <cmath>

作为标题。

这篇关于M_PI被标记为未声明的标识符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆