【问题标题】:Pow: ambiguous call to overloaded function [duplicate]Pow:对重载函数的模糊调用[重复]
【发布时间】:2016-03-26 12:18:12
【问题描述】:

我有问题,我找不到任何解决方案。

它给出了同样的错误:

Pow:对重载函数的模糊调用

#include <stdio.h>
#include <math.h>

int main() 
{
    int a, i, n, product, result=1;

    printf("enter a number\n");
    scanf("%d", &a);

    printf("enter n number\n");
    scanf("%d", &n);

    for(i = 1; i < n; i++) {
        product = pow(a, i);
        result *= product;
    }
    printf("the result is %d", result);

    return 0;
}

【问题讨论】:

标签: c pow math.h


【解决方案1】:

主要问题是未初始化的变量,即result,它没有在行中使用的有效值:

result *= product;  

然后在函数pow()的使用中进行一些隐式转换。没有重载的pow() 实例包含intint 作为第一个和第二个参数,int 作为返回值。

你应该考虑根据函数支持的参数列表适当地定义你的参数,在C中,使用标题math.h是:

double pow(double x, double y)

否则您可能会遭受有时会产生意外结果的后果,因为编译器会隐式执行缩小转换。检查隐式转换的危险here

【讨论】:

    猜你喜欢
    • 2014-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-11
    • 2011-12-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多