Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,670 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,164 people online right now. Registration is fast and FREE... Join Now!




error C2668: 'log10' : ambiguous call to overloaded

 
Reply to this topicStart new topic

error C2668: 'log10' : ambiguous call to overloaded, ambiguous call to overloaded

pac213
post 3 Sep, 2007 - 08:59 AM
Post #1


New D.I.C Head

*
Joined: 3 Sep, 2007
Posts: 2


My Contributions


CODE

#include <cmath>
#include <malloc.h>
#include <iostream.h>
#include <process.h>
#include <stdio.h>
using namespace std;
void main()


    {
        unsigned int d;    //number of digits
        unsigned char *a;
        unsigned int j, n, q, z, t;
        int i;
        double p;
        
        cout << "\nEnter the No\t:";
        cin >> n;
        p = 0.0; //calculate the number of digits required
        for(j = 2; j <= n; j++)
            p += log10(j);
        d = (int)p + 1;
        a = (unsigned char*)malloc(d*sizeof(unsigned char));
        if (!a)


            {
                cout << "Could not allocate memory!!!";
                exit(0);
            }
            for (i = 1; i < d; i++)
                a[i] = 0; //initialize
            a[0] = 1;
            p = 0.0;
            for (j = 2; j <= n; j++)


                {
                    q = 0;    //initialize remainder to be 0
                    p += log10(j);
                    z = (int)p + 1;
                    for (i = 0; i <= z/*NUMDIGITS*/; i++)


                        {
                            t = (a[i] * j) + q;
                            q = (t / 10);
                            a[i] = (char)(t % 10);
                        }
                    }
                    cout << "\nThe Factorial is\n";
                    // the fact is stored ulta..
                    for( i = d -1; i >= 0; i--)


                        {
                            cout << (int)a[i];
                        }
                }


whenever i run this i always get this errors

1>c:\documents and settings\god\my documents\visual studio 2005\projects\fack\fack\fack.cpp(21) : error C2668: 'log10' : ambiguous call to overloaded function
1> c:\program files\microsoft visual studio 8\vc\include\math.h(569): could be 'long double log10(long double)'
1> c:\program files\microsoft visual studio 8\vc\include\math.h(521): or 'float log10(float)'
1> c:\program files\microsoft visual studio 8\vc\include\math.h(122): or 'double log10(double)'
1> while trying to match the argument list '(unsigned int)'
1>c:\documents and settings\god\my documents\visual studio 2005\projects\fack\fack\fack.cpp(31) : warning C4018: '<' : signed/unsigned mismatch
1>c:\documents and settings\god\my documents\visual studio 2005\projects\fack\fack\fack.cpp(40) : error C2668: 'log10' : ambiguous call to overloaded function
1> c:\program files\microsoft visual studio 8\vc\include\math.h(569): could be 'long double log10(long double)'
1> c:\program files\microsoft visual studio 8\vc\include\math.h(521): or 'float log10(float)'
1> c:\program files\microsoft visual studio 8\vc\include\math.h(122): or 'double log10(double)'
1> while trying to match the argument list '(unsigned int)'
1>c:\documents and settings\god\my documents\visual studio 2005\projects\fack\fack\fack.cpp(42) : warning C4018: '<=' : signed/unsigned mismatch

ive tried some tips, but it didnt help me...
User is offlineProfile CardPM

Go to the top of the page

Bench
post 3 Sep, 2007 - 09:34 AM
Post #2


D.I.C Addict

Group Icon
Joined: 20 Aug, 2007
Posts: 602



Thanked 10 times

Dream Kudos: 150

Expert In: C/C++

My Contributions


The compiler is telling you that log10 doesn't know what to do with an int.

log10 has 3 available ways to cast an int to either a float, double, or long double, and doesn't know which one to pick. For your program, it probably doesn't really matter which one
(double is the 'default' floating point type, so double will do)

try this instead
CODE

  log10( static_cast<double> (j) );

where j is your int variable - static_cast allows it to impersonate a variable of double type.

The same applies for many other functions in the <cmath> header. (such as sqrt, sin, cos, etc)
User is offlineProfile CardPM

Go to the top of the page

barnwillyb
post 3 Sep, 2007 - 02:33 PM
Post #3


D.I.C Head

**
Joined: 22 May, 2007
Posts: 55


My Contributions


QUOTE(pac213 @ 3 Sep, 2007 - 09:59 AM) *

CODE

#include <cmath>
#include <malloc.h>
#include <iostream.h>
#include <process.h>
#include <stdio.h>
using namespace std;
void main()


    {
        unsigned int d;    //number of digits
        unsigned char *a;
        unsigned int j, n, q, z, t;
        int i;
        double p;
        
        cout << "\nEnter the No\t:";
        cin >> n;
        p = 0.0; //calculate the number of digits required
        for(j = 2; j <= n; j++)
            p += log10(j);
        d = (int)p + 1;
        a = (unsigned char*)malloc(d*sizeof(unsigned char));
        if (!a)


            {
                cout << "Could not allocate memory!!!";
                exit(0);
            }
            for (i = 1; i < d; i++)
                a[i] = 0; //initialize
            a[0] = 1;
            p = 0.0;
            for (j = 2; j <= n; j++)


                {
                    q = 0;    //initialize remainder to be 0
                    p += log10(j);
                    z = (int)p + 1;
                    for (i = 0; i <= z/*NUMDIGITS*/; i++)


                        {
                            t = (a[i] * j) + q;
                            q = (t / 10);
                            a[i] = (char)(t % 10);
                        }
                    }
                    cout << "\nThe Factorial is\n";
                    // the fact is stored ulta..
                    for( i = d -1; i >= 0; i--)


                        {
                            cout << (int)a[i];
                        }
                }


whenever i run this i always get this errors

1>c:\documents and settings\god\my documents\visual studio 2005\projects\fack\fack\fack.cpp(21) : error C2668: 'log10' : ambiguous call to overloaded function
1> c:\program files\microsoft visual studio 8\vc\include\math.h(569): could be 'long double log10(long double)'
1> c:\program files\microsoft visual studio 8\vc\include\math.h(521): or 'float log10(float)'
1> c:\program files\microsoft visual studio 8\vc\include\math.h(122): or 'double log10(double)'
1> while trying to match the argument list '(unsigned int)'
1>c:\documents and settings\god\my documents\visual studio 2005\projects\fack\fack\fack.cpp(31) : warning C4018: '<' : signed/unsigned mismatch
1>c:\documents and settings\god\my documents\visual studio 2005\projects\fack\fack\fack.cpp(40) : error C2668: 'log10' : ambiguous call to overloaded function
1> c:\program files\microsoft visual studio 8\vc\include\math.h(569): could be 'long double log10(long double)'
1> c:\program files\microsoft visual studio 8\vc\include\math.h(521): or 'float log10(float)'
1> c:\program files\microsoft visual studio 8\vc\include\math.h(122): or 'double log10(double)'
1> while trying to match the argument list '(unsigned int)'
1>c:\documents and settings\god\my documents\visual studio 2005\projects\fack\fack\fack.cpp(42) : warning C4018: '<=' : signed/unsigned mismatch

ive tried some tips, but it didnt help me...



main function needs to return 0.

#include <iostream>
#include<cmath>

using namespace std;

unsigned int d; //number of digits
unsigned char *a;
unsigned int j, n, q, z, t;
int i;
double p;

cout << "\n\tEnter the No: ";
cin >> n;

p = 0.0; //calculate the number of digits required
for(j = 2; j <= n; j++)
p += log10(j);

d = (int)p + 1;
a = (unsigned char*)malloc(d*sizeof(unsigned char));

if (!a) {
cout << "\tCould not allocate memory!!!";
exit(0);
}

for (i = 1; i < d; i++)
a[i] = 0; //initialize
a[0] = 1;
p = 0.0;

for (j = 2; j <= n; j++) {
q = 0; //initialize remainder to be 0
p += log10(j);;
z = (int)p + 1;
for (i = 0; i <= z/*NUMDIGITS*/; i++) {
t = (a[i] * j) + q;
q = (t / 10);
a[i] = (char)(t % 10);
}
}

cout << "\tThe Factorial is ";
// the fact is stored ulta..
for( i = d -1; i >= 0; i--) {
cout << (int)a[i];
}
cout << ".\n";

return 0;
}

Hope that helps! I am on a mac and the above program works in Xcode.
barnwillyb
User is offlineProfile CardPM

Go to the top of the page

pac213
post 4 Sep, 2007 - 08:55 AM
Post #4


New D.I.C Head

*
Joined: 3 Sep, 2007
Posts: 2


My Contributions


QUOTE(Bench @ 3 Sep, 2007 - 10:34 AM) *

The compiler is telling you that log10 doesn't know what to do with an int.

log10 has 3 available ways to cast an int to either a float, double, or long double, and doesn't know which one to pick. For your program, it probably doesn't really matter which one
(double is the 'default' floating point type, so double will do)

try this instead
CODE

  log10( static_cast<double> (j) );

where j is your int variable - static_cast allows it to impersonate a variable of double type.

The same applies for many other functions in the <cmath> header. (such as sqrt, sin, cos, etc)


thanks, it worked now biggrin.gif
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/23/08 05:58AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month