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

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




PrimeNumbers\\error C2668

 
Reply to this topicStart new topic

PrimeNumbers\\error C2668

climax
post 2 Sep, 2007 - 02:53 AM
Post #1


New D.I.C Head

*
Joined: 21 Aug, 2007
Posts: 21


My Contributions


Hi there...
I'm working on a program that writes out all the prime numbers up to 1000. I think I got the program almost right, but when i compile it i get the C2668 error ('sqrt' : ambigious call to overloaded function). Can anyone see what might be the problem here? if so, thanks alot biggrin.gif

CODE

#include <iostream>
#include <math.h>

using namespace std;

bool isPrime (int num)
{

    // no number can divide into 0
    if(num == 0)
    return true;

    // make sure its not negative
    num = abs(num);

    for(int i=2; i<=sqrt(num); i++)
    {
    if(num % i == 0)
    {
        return false;
    }  
    return true;  
    }
}
int main()
{
    for (int i=2;i<1000;i++)
    {
        bool number = isPrime(i);;
        if (number == true)
        {
            cout<<i<<"\t";
        }
    }
    cout<<"\n"<<endl;
}
User is offlineProfile CardPM

Go to the top of the page

Bench
post 2 Sep, 2007 - 03:02 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


sqrt isn't designed to take int, but it has 3 available overloads where 'int' is allowed to implicitly cast, so the call is ambiguous - If you provide an explicit cast, the compiler will know which one to use.

CODE
    for(int i=2; i<=sqrt(  static_cast<double>(num) ); i++)  



edit, On a minor point, you could change <math.h> , which is a deprecated header in C++, to <cmath> to ensure you're using the latest standard version of the 'C' math header.


By the way, all your isprime() function seems to do is output odd numbers. have another think about how you could find prime numbers (You might find this idea interesting.. http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes )

This post has been edited by Bench: 2 Sep, 2007 - 03:11 AM
User is offlineProfile CardPM

Go to the top of the page

climax
post 2 Sep, 2007 - 03:08 AM
Post #3


New D.I.C Head

*
Joined: 21 Aug, 2007
Posts: 21


My Contributions


QUOTE(Bench @ 2 Sep, 2007 - 04:02 AM) *

sqrt isn't designed to take int, but it has 3 available overloads where 'int' is allowed to implicitly cast, so the call is ambiguous - If you provide an explicit cast, the compiler will know which one to use.

CODE
    for(int i=2; i<=sqrt(  static_cast<double>(num) ); i++)  



edit, On a minor point, you could change <math.h> , which is a deprecated header in C++, to <cmath> to ensure you're using the latest standard version of the 'C' math header.



ah.. nice smile.gif that solved the c2668 error....
User is offlineProfile CardPM

Go to the top of the page

climax
post 2 Sep, 2007 - 03:35 AM
Post #4


New D.I.C Head

*
Joined: 21 Aug, 2007
Posts: 21


My Contributions


QUOTE(Bench @ 2 Sep, 2007 - 04:02 AM) *

By the way, all your isprime() function seems to do is output odd numbers. have another think about how you could find prime numbers (You might find this idea interesting.. http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes )


thnx again! got the function right now.. had to change out a bit yea wink2.gif
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/23/08 06:37AM

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