From the Textbook

What does this code do? Exercise:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//Exercise 8.14 WDTCD
#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

int main()
{

int power=5;
int number=10;

cout << endl;

for (int counter = 1; counter <= power; counter++ )
{
	cout<<pow( number, counter ) << endl;
}//end for

cout<<"\n";

return 0;
}//end main  


Error Message:Error 1 error C2668: 'pow' : ambiguous call to overloaded function e:\intro2programming\section8\wdtcd\wdtcd.cpp 18
Any ideas?
there are 3 different versions of pow. One takes float, one takes double, and another takes long double. You are using ints, which is neither of those three, so the compiler needs to cast your ints into a different type. However it doesn't know which type you want, and so doesn't know which version of pow to call.

The solution here is to not use ints for number, but instead use double or float. OR, you can explicitly tell the compiler which one you want by casting:

 
cout << pow( static_cast<double>(number), counter ) << endl;


PS: don't you just love it when examples from textbooks fail to compile? I swear I don't understand how those people have jobs. Don't they test this stuff?
Last edited on
Can you tell I am getting ready for finals?
Topic archived. No new replies allowed.