首页 > 解决方案 > 为什么我在终端中收到 cmath 错误?

问题描述

我在终端中编译我的代码时遇到了问题,特别是针对 cmath 的错误。

这是错误代码。

area_calculator.cpp:45: error: call of overloaded `pow(int&, int)' is ambiguous
/usr/include/bits/mathcalls.h:154: error: candidates are: double pow(double, double)
/usr/include/g++/cmath:512: error: long double std::pow(long double, int)
/usr/include/g++/cmath:508: error: float std::pow(float, int)
/usr/include/g++/cmath:504: error: double std::pow(double,int)
/usr/include/g++/cmath:495: error: long double std::pow(long double, long double)
/usr/include/g++/cmath:486: error: float std::pow(float,float)

当我在 cpp.sh 中运行代码时,它运行良好,但编译后出现错误。

这是我的代码

#include <iomanip>
#include <iostream>
#include <cmath>

int main() {

    int optionNumber;
    int radius;
    const double pi = 3.14159;
    double circleArea;

    int length, width;
    double rectangleArea;

    int base, height;
    double triangleArea;

    if (n == 1) {

        std::cout << std::fixed;
        std::cout << std::setprecision(2);
        circleArea = pi * pow(radius, 2);

    } else if (n == 2) {

        std::cout << std::fixed;
        std::cout << std::setprecision(2);
        rectangleArea = length * width;


    } else if (n == 3) {

        std::cout << std::fixed;
        std::cout << std::setprecision(2);
        triangleArea = (base * height * 0.5);
    }
}

当然,由于这是家庭作业,因此缺少一些代码,但我认为这应该会有所帮助。

提前致谢。

标签: c++cmath

解决方案


推荐阅读