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.
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.
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...
Related
When i want to compile a code, is giving me an error... cout and cin was not declared in this scope. What's the problem?
I searched on google. They said i need to reinstall codeblocks. I have done this and is not working.
#include <iostream>
int main()
{
int n,z,c;
cin>>n;
z=0;
while(n>0)
{
c=n%10;
n=n/10;
if(c<5)
{
z=z*10+2*c;
}
}
cout << z;
return 0;
}
It should compile it...
Just add this using namespace std; after #include <iostream> . Or use std::cin std::cout.
Also posting the 3rd way (compromise between the 2 existing answers - was already mentioned in the comments), which I think works best for the current scenario. This is my favorite one (well, excepting cases when I use lots of stuff from a namespace).
Add:
using std::cin;
using std::cout;
after the #include. This way:
You avoid using namespace X; hell. That's a big NO-NO, there are lots of resources explaining why (you could check [SO]: what is the reason for using a wildcard import? (#CristiFati's answer) for an equivalent in Python)
You don't have to type the fully qualified name every time (just the plain name). Using FQNs can be / is:
Very annoying (especially when dealing with nested namespaces)
Safe
Easier to read
Addding std::cin or std::cout would fix it
If you don't want to add std:: again and again then
You can also add using namespace std; just after #include<iostream>
This happens because cin and cout are members of standard library.
This question already has answers here:
Why are some functions in <cmath> not in the std namespace?
(3 answers)
Closed 9 years ago.
I have a fairly large application and I am working without the std namespace, I noticed I wasn't including std::cos or std::sin yet I am getting the right results. Why?
An example of some cut down code would be:
#include <ctime>
#include <cmath>
#include <iostream>
#include <vector>
//#include <unistd.h>
#include <fstream>
#include <sstream>
#include <iomanip>
using std::cout;
using std::endl;
int main()
{
double pi = 4*(atan(1));
cout << "pi = " << pi << endl
<< "cos(pi) = " << cos(pi) << endl
<< "sin(pi) = " << sin(pi) << endl;
return 0;
}
I have left all the headers in, I am using them all in the main code. The output returns ~3.14, -1 and 1e-16 as expected. Why does this work? cos and sin are in std aren't they?
I'm using the g++ compiler on a remote unix server
Thanks
When you include <cmath>, all of the functions are declared in
std::. For the C headers, there is also a special rule, which
allows (but doesn't require) the implementation to make them
visible in the global namespace; this is because most
implementations will simply adapt the C headers, something like:
#include <math.h>
namespace std
{
using ::sin;
using ::cos;
// ...
}
This is an obvious way of implementing the library without
having to rewrite everything, just to have it in C++, and it
will result in all of the names also being present in the global
namespace.
Formally, this is a C++11 feature; pre-C++11 required that
<cmath> only introduce the symbols into std::. Practically,
all, or at least most implementations did something like the
above, and did introduce them, illegally, into the global
namespace, so C++11 changed the standard to reflect reality.
Unfortunately, library implementations are allowed to dump names from the C library into the global namespace as well as std, and many do. Even worse, in some cases only some overloads are available in the global namespace, leading to unexpected loss of precision if you don't specify the std version.
You should always use the std versions, but sadly there's no reliable way to enforce that, so you'll just have to tread carefully through this particular minefield.
I'm trying to start using namespaces the correct (or at least best) way.
The first thing I tried to do was to avoid putting using namespace xxx; at the beginning of my files. Instead, I want to using xxx::yyy as locally as possible.
Here is a small program illustrating this :
#include <iostream>
#include <cstdlib>
#include <ctime>
int main() {
using std::cout;
using std::endl;
srand(time(0));
for(int i=0; i<10;++i)
cout << rand() % 100 << endl;
return 0;
}
If I omit the lines using std::cout; or using std::endl, the compiler will complain when I'm trying to use cout or endl.
But why is this not needed for srand, rand and time ? I'm pretty sure they are in std, because if I try to specifically pour std:: in front of them, my code is working fine.
If you use cstdlib et al. the names in them are placed in both the global and the std:: namespaces, so you can choose to prefix them with std:: or not. This is seen as a feature by some, and as a misfeature by others.
If you really want to know, take a close look at the ctime and cstdlib headers. They were built backwards-compatible.
Note: all this using vs. using namespace business is about readability. If your IDE would allow to just not show the namespaces when you don't want to see them, you wouldn't need these constructs...
I prefer to omit using and just have the std::cout every time just to maintain readability. although this is probably only useful in larger projects
As long as we on the subject, there's also a thing called Koenig Lookup which allows you to omit a namespace identifier before a function name if the arguments it take come from the same namespace.
For example
#include <iostream>
#include <algorithm>
#include <vector>
void f(int i){std::cout << i << " ";}
int main(int argc, char** argv)
{
std::vector<int> t;
// for_each is in the std namespace but there's no *std::* before *for_each*
for_each(t.begin(), t.end(), f);
return 0;
}
Well, it's not related directly but I though it may be useful.
I just realized that I am supposed to include the #include<cstdlib> required by abs() for the abs() function.
#include<iostream>
using namespace std;
int main()
{
int result;
result = abs(-10);
cout << result << "\n";
return 0;
}
Why does this code still work, even though I forgot the important header (#include<cstdlib>)?
That's because iostream indirectly includes definition for abs(). It is allowed by the Standard, but should not be relied upon, because it's implementation-dependant (i.e. your code may not compile on some other compilers).
I am new to C++ programming.
So I was trying my luck executing some small programs.
I am working on HP-UX which has a compiler whose
executable is named aCC.
I am trying to execute a small program
#include <iostream.h>
using namespace std;
class myclass {
public:
int i, j, k;
};
int main()
{
myclass a, b;
a.i = 100;
a.j = 4;
a.k = a.i * a.j;
b.k = 12;
cout << a.k << " " << b.k;
return 0;
}
When I compile this it gives me an error:
> aCC temp.cpp
Error 697: "temp.cpp", line 2 # Only namespace names are valid here.
using namespace std;
^^^
What exactly is the problem?
Is std not considered as a namespace in the aCC compiler or is there some serious drawback with aCC?
If I change the <iostream.h> to <iostream>, I get some more errors added as below.
>aCC temp.cpp
Error 112: "temp.cpp", line 1 # Include file <iostream> not found.
#include <iostream>
^^^^^^^^^^
Error 697: "temp.cpp", line 2 # Only namespace names are valid here.
using namespace std;
^^^
Error 172: "temp.cpp", line 14 # Undeclared variable 'cout'.
cout << a.k << " " << b.k;
Which version of aCC are you using? Older versions used a pre-standard STL implemenntation that put everything in the global namespace (i.e. didn't use the std namespace)
You might also need to use the -AA option when compiling. This tells the compiler to use the newer 2.x version of HP's STL library.
>aCC -AA temp.cpp
And it should always be
<iostream>
<iostream.h>
is from a pre-standard implementation of the language, though it is usually shipped so as to maintain backwards compatibility with older code.
Try with:
#include <iostream>
Instead of:
#include <iostream.h>
iostream.h is an old style header in which all functions are exposed in global namespace. naturally in such a case, using namespace std may not work since std namespace is probably not exposed by iostream.h header (in this compiler). As explained above, try with # include which is a new style C++ standard library header. (thanks Shailesh Kumar for the comment! included it in the answer).