2015-01-10 106 views
0

我试图创建一个十进制到二进制转换器的辅助功能,它会告诉我的基础上,2POW函数C++错误

这最高权力开始这一点是我的代码:

int determineStart(int val){ 
    int topval = 1; 
    int i = 0; 
    for(i = 0; val > 2 * topval; ++i){ 
     topval = pow(2, i); 
    } 
    return topval; 
} 

我得到以下错误: main.cpp:9: error: call of overloaded pow(int, int&) is ambiguous

我试着切换周围几件事情(如在double或单独的变量将作为第二个参数有关pow(),使得topval a double等),但似乎无法让它工作。

我在做什么错?

+0

在程序中还有其他'pow'定义?请发布完整的程序 – bluefog

回答

2

获得的两个大国的最简单,最快的方式正在发生变化 - 在CPU无论如何都是使用二进制的,所以它可以为你添加一些额外的零。

因此,而不是pow(2, i)你会写(1 << i)

1

这种参数类型没有pow()函数。它可以是(float, int),(double, int)(long double, int)。如果你用(int, int)调用它,编译器不知道上面哪些类型使用。最简单的办法是做:

pow(2.0, i) 

编辑: 我假设你使用标准pow()定义从<cmath><math.h>

+0

以及如何解释[this](http://ideone.com/ndcIrY) – bluefog

+0

我使用Visual Studio 2010,并且出现错误:对重载函数的模糊调用。 – Avert

+1

@ShikharBhardwaj在C++ 11中增加了更多的重载。 –

1

铸造可能是您的错误:您可以轻松地将int转换为double或float。 试试这个:

pow(2.0, i) 

它会像:

pow((double)(2), i)