C++错误:标准头文件引用错误


1、背景

  现希望引用标准头文件来计算4的根号和平方,编写如下代码,但发现引入标准头文件错误。

#include <math>
#include <iostream>
using namespace std;

int main()
{
    cout << "根号:" << sqrt(4) << endl;
    cout << "平方:" << pow(4, 2) << endl;
    return 1;
}

错误提示

2、分析与修改

  C语言一般通过#include <***.h>引用标准库文件,而C++一般通过#include <c**>引用标准库文件。#include <math>中的math即不是math.h,也不是cmath,故引用错误。

// 错误
#include <math>
// 修改
#include <math.h>  // c
#include <cmath>   // c++

修改后的正确结果:

猜你喜欢

转载自blog.csdn.net/qq_34801642/article/details/106567591
今日推荐