-1

Here is my code. I am using Dev-C++ 4.9.8.0 and I can not figure out why this wont compile.

#include <iostream>
#include <cmath>
#include <stdlib.h>

using namespace std;

int main() {
    int  n;    // Number to test for prime-ness
    int  i;    // Loop counter
    int  is_prime = true; // Boolean flag...
                          // Assume true for now.

    // Get a number form the keyboard.

    cout << "Enter a number and press ENTER: ";
    cin >> n;

    // Test for prime by checking for divisibility 
    // by all whole numbers from 2 to sqrt(n).

    i = 2;
    while (i <= sqrt(n)) {  // While i is <= sqrt(n),
         if (n % i == 0)         // If i divides n, 
             is_prime = false;   //    n is not prime.
         i++;
 }

 // Print results

 if (is_prime)
     cout << "Number is prime." << endl;
 else
     cout << "Number is not prime." << endl;

 system("PAUSE");   
 return 0;
}

Im getting various error messages about overloading. Can someone please help me figure out why its not compiling correctly.

  • 8
    Try posting the error messages you're seeing. – Retired Ninja Dec 31 '13 at 19:26
  • 3
    For one thing, `` should either be ``, or you should bracket that include in `extern "C"` – Andrey Mishchenko Dec 31 '13 at 19:28
  • I am not sure but `int is_prime = true;` this legal ???.. how you can give string value to int ??? – Ashish Dec 31 '13 at 19:29
  • 4
    For another, `using namespace std;` is a terrible idea in general and can lead to errors of the type you're describing because of collisions with commonly-used symbol names in `std::`. – Andrey Mishchenko Dec 31 '13 at 19:29
  • 2
    @Ashish `int is_prime= true;` is absolutely legal. – Kaustav Ray Dec 31 '13 at 19:32
  • 1
    Compiles and runs with g++ 4.7.3 under ubuntu 13.04. – Tobias Dec 31 '13 at 19:33
  • 1
    @Ashish `true` and `false` are constants that can be promoted to `int` implicitly, though, it would be better if `is_prime` was declared as a `bool`. – Zac Howland Dec 31 '13 at 19:34
  • 3
    Dev-C++ 4.9.8.0 is 10 years old. Please update your compiler. –  Dec 31 '13 at 19:34
  • Are you trying to compile any other code with this? If there's another main() somewhere that could explain the overloading error. – Lokno Dec 31 '13 at 19:35
  • [Warning] In function 'int main()': – user3150065 Dec 31 '13 at 19:38
  • @ZacHowland Thanks.. I could have still build an enumerator & sorted out this like `enum bool { true,false }` some sort of this in dev c++ – Ashish Dec 31 '13 at 19:38
  • line 22 call of overloaded 'sqrt(int&)' is ambiguous – user3150065 Dec 31 '13 at 19:38
  • line 163 C:\Dev-Cpp\include\math.h candidates are: double sqrt(double) – user3150065 Dec 31 '13 at 19:40
  • line 465 C:\Dev-Cpp\include\c++\cmath long double std::sqrt(long – user3150065 Dec 31 '13 at 19:41
  • I tried using #include and still gave the above error messages – user3150065 Dec 31 '13 at 19:42
  • line 461 C:\Dev-Cpp\include\c++\cmath float std::sqrt(float) – user3150065 Dec 31 '13 at 19:43
  • C:\Dev-Cpp\Makefile.win [Build Error] [prime1.o]Error 1 – user3150065 Dec 31 '13 at 19:44
  • @Ashish `enum bool { ... }` is invalid in C++ as `bool` is a keyword for a boolean type. `bool`, `true`, and `false` are all reserved words in C++. – Zac Howland Dec 31 '13 at 19:51
  • @ZacHowland that was just for understanding takeit bool1 if you want – Ashish Dec 31 '13 at 19:53
  • 1
    @Ashish In that case, yes, you could have `enum BOOLEAN { FALSE = 0, TRUE }` and something like `int i = TRUE` would compile with no problems. – Zac Howland Dec 31 '13 at 19:57
  • 2
    @user3150065 - DevCpp is *ancient*. For your own sake, please switch to a modern compiler. I'll assume you're on Windows, so I would go with Visual Studio or clang. You may be wondering: "why should I care that my compiler is ancient?", the answer is that you are stuck with C++98 (which is a terrible language now that we know better). C++11 "feels like a completely different language" to quote the gurus, it looks and feels much much nicer, it's harder to write bugs, and it's easier to be productive. Modern compilers enable you to use all of the new features. – Mark Dec 31 '13 at 20:09

2 Answers2

8

As predicted, the error is a result of a symbol clash between an std::sqrt and sqrt, due to your use of using namespace std;.

The header cmath has a function called std::sqrt, and the symbol name sqrt is being imported into your namespace because of your using namespace std;. Even though you are not including math.h, for some reason your compiler is importing that header as well, and math.h defines a sqrt function.

The compiler is complaining that it does not know which sqrt to use.

The correct solution to this is to not use using namespace std;. See also: Why is "using namespace std" considered bad practice?.

In your particular case, you can replace using namespace std; with the following:

using std::cout;
using std::cin;
using std::endl;

to avoid typing std:: in front of these all the time.

To be honest the compiler should not be including math.h, and as others have pointed out, using a 10+ year old compiler is just silly. Use a modern compiler.

Edit: Also, please, never again post half a dozen comments in a row to communicate a multi-line error message. Just edit your original post.

Community
  • 1
  • 1
Andrey Mishchenko
  • 3,986
  • 2
  • 19
  • 37
0

This compiles fine in gcc.

Although thre are some things you can improve like not including #include <stdlib.h> including stdlib instead of stdlib.h and making is_prime bool.

line 22 call of overloaded 'sqrt(int&)' is ambiguous

try sqrt<int>(n) or sqrt((int) n)

@Andrey gives you the answer: use ::sqrt(n) or std::sqrt(n) or include math.h instead of cmath. The best is still as he suggests: don't use using namespace std;.

My advice: switch to a more mainstream compiler like gcc, clang or Visual Studio. They better conform with the standard.

The book i am using uses Dev-C++

i don't want to be mean but switch to another book two. I wouldn't trust a book that makes you include stdlib.h. It's a header from the time that C wasn't standardized yet. So... yeah... switch the book...

bolov
  • 72,283
  • 15
  • 145
  • 224