3

I have problem , I can not find any solution.

It gives the same error:

Pow: ambiguous call to overloaded function

#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;
}
  • 1
    you have to initialize `result=1` otherwise it will use garbage value – Punit Vara Dec 20 '15 at 06:54
  • 10
    you compile as C++. change to C mode. – BLUEPIXY Dec 20 '15 at 07:08
  • 2
    Would this help: [Building libspline for Matlab on Windows - ambiguous call to overloaded function 'pow'](http://stackoverflow.com/questions/17633970/building-libspline-for-matlab-on-windows-ambiguous-call-to-overloaded-function)? – awesoon Dec 20 '15 at 07:54
  • @BLUEPIXY it did not work –  Dec 20 '15 at 10:46
  • @punitvara good point thanks but it did not work –  Dec 20 '15 at 10:46
  • 1
    I can compile with no error (there is warning. :D). Whether the program is working properly is another matter. – BLUEPIXY Dec 20 '15 at 10:52
  • 1
    Selected C++ duplicate does not well explain the failure here. Nominate for re-opening. – chux - Reinstate Monica Dec 20 '15 at 15:26
  • @chux Poor use of the dupe hammer in my view. The asker is actually compiling C++ and the dupe explains it perfectly. – David Heffernan Dec 20 '15 at 16:36
  • @chux: agree with Devid Heffernan. There are no overloads in C; the compiler cannot be a C compiler. The duplicate question explains the issue nicely — the compiler messages in the question show that passing an `int` as the first argument to `pow()` in C++ leads to ambiguity because `int` has to be converted and can be converted to `float`, `double` or `long double` with equal facility, leading to the ambiguity. The fact that people end up using a C++ compiler to compile C code is a separate source of exasperation — I assume they're confused by the IDE that they're using. – Jonathan Leffler Dec 20 '15 at 17:06
  • 1
    @Jonathan Leffler If this post is viewed as tagged C, solutions include using type-generic math with `` with acts very similar to C++ overloads. I agree this is certainly OP using a C++ compiler on C code. A user having problems with `pow()` and filtering on `C` will not find that duplicate as it is another language. The candidate solutions in C differ from C++ and thus I still assert the claimed duped is a poor candidate. – chux - Reinstate Monica Dec 20 '15 at 18:08
  • @chux: we are going to have to agree to disagree. If there was any sign of `#include ` in the code I might be more sympathetic. As it is, compiling code with a C++ compiler means you get C++ error messages for problems that only appear when you use a C++ compiler instead of a C compiler — regardless of why you are using the wrong compiler. – Jonathan Leffler Dec 20 '15 at 18:17
  • @David Heffernan How did you determine the original duplicate after the post was re-opened? – chux - Reinstate Monica Dec 20 '15 at 18:18
  • @chux You can see that in the edit history – David Heffernan Dec 20 '15 at 19:01
  • 1
    @David Heffernan Thanks. Concerning the [original duplicate](http://stackoverflow.com/q/17633970/2410359), that was a C++ one where the OP did not include `` and so I disagree that [the dupe explains it perfectly.](http://stackoverflow.com/questions/34378487/pow-ambiguous-call-to-overloaded-function?noredirect=1#comment56507783_34378487). Perhaps you thought that my vote was on another dupe? – chux - Reinstate Monica Dec 20 '15 at 19:31
  • @chux I honestly don't understand why you continue to assert that this is a C question. C does not have overloading. I'm sure you know this. – David Heffernan Dec 20 '15 at 19:56
  • 1
    @David Heffernan This is a C question because the OP called it C, likely wanted it to be C and posted code has many hallmarks of good C (`.h, scanf(), printf()`) vs. C-ish C++. OP's problem was likely not using a C compiler. 2) C _does_ have something very similar to overloading. This is possible with `_Generic` and is purposely available with `` which would make C source code `pow(float)` and `pow(double)` call different functions. As OP's message is with `pow()`, that consideration is relevant. – chux - Reinstate Monica Dec 20 '15 at 20:14
  • @chux Like Jonathan, I disagree with you – David Heffernan Dec 20 '15 at 20:30
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/98498/discussion-between-chux-and-david-heffernan). – chux - Reinstate Monica Dec 20 '15 at 20:44

1 Answers1

0

Main problem is uninitialized variables, namely result that doesn't have a valid value to be used in the line:

result *= product;  

and then some implicit conversions in the use of function pow(). There isn't a overloaded pow() instance containing int and int as first and second parameter, and int as returning value.

You should consider defining your arguments appropriately according to function supported argument list, which in C, using header math.h is:

double pow(double x, double y)

otherwise you may suffer the consequences of sometimes producing unexpected results, as the compiler performs narrowing conversions implicitly. Check for the dangers of implicit conversions here.

Ziezi
  • 6,375
  • 3
  • 39
  • 49