Help - why is there an issue with the power function?
the error states:
: error C2668: 'pow' : ambiguous call to overloaded function

void compute(int integer)
{
	cout << integer;
	int value = 9;

	while(value > 0)
	{
		cout << ++ integer;
		int square = pow(integer,2);
		cout << square;
	}
}

Dani AI

Generated

The compiler error comes from overload resolution, not from a bug in your loop. Several overloads of pow exist (float/double/long double versions); with two plain int arguments the compiler sees multiple equally good conversions and reports an ambiguous call. ’s tip to make the call unambiguous is correct, but for integer squares using pow is unnecessary and can introduce floating-point rounding and conversion work.

A simple, robust approach is to compute squares with integer arithmetic and, if you really want the numbers first and the squares afterwards while keeping a single loop that generates values, accumulate the squares as you print the numbers and emit them after the loop. This avoids pow entirely and gives exact results:

#include <iostream>
#include <sstream>

void compute_and_print(int start, int count) {
    std::ostringstream sqbuf;
    int i = start;
    int remaining = count;
    while (remaining-- > 0) {
        std::cout << i << ' ';
        sqbuf << (i * i) << ' ';
        ++i;
    }
    std::cout << '\n' << sqbuf.str() << '\n';
}

Notes and troubleshooting:

  • Prefer i * i for integer powers of 2: faster, exact, and avoids double -> int casts.
  • If you must use pow, include <cmath> and call std::pow with at least one floating argument (or use static_cast) so overload resolution picks the floating overload; be aware it returns a floating type and may round.
  • For large values use a wider integer type (e.g., long long) to avoid overflow when squaring.

Summary: use integer multiplication for squares and buffer the second row if you need the numbers first. That meets the “one function / one while loop” constraint while keeping output ordered and correct.

Recommended Answers

All 4 Replies

pow is overloaded for multiple types, and all of them can be converted to from int. An easy way to fix the problem is to force the second argument to double:

int square = pow(integer,2.0);

pow is overloaded for multiple types, and all of them can be converted to from int. An easy way to fix the problem is to force the second argument to double:

int square = pow(integer,2.0);

ok thank you - at first it was giving me another error but now it is working fine. Do you know if there is a way to combine this using only one function and one while loop? I tried doing it but it was integrating the squares with the integers and it needs to print 1 through 10 and then the squares, 1 through 100.

void compute(int integer)
{
	int counter = 9;
	while(counter >= 0)
	{
		cout << integer << " ";
		integer++;
		counter -=1;
	}
}
void square(int integer)
{
	int counter2 = 9;
	while(counter2 >= 0)
	{
		cout << pow(integer,2.0) << " ";
		integer++;
		counter2 -=1;
	}
}

If you don't mind two columns instead of two rows then this works:

void square(int integer)
{
	int counter2 = 9;
	while(counter2 >= 0)
	{
		cout << integer << '\t' << pow(integer,2.0) << '\n';
		integer++;
		counter2 -=1;
	}
}

ok thank you :-D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.