Hey guys, cool site, glad to be a member! I have a new C++ book that I'm learning from and I can't figure out what I'm missing. Here is the function straight out of "C++ without Fear" second edition by Brian Overland.
void get_divisors(int n){
int i;
double root_n = sqrt(n);
for(i=2; i<=root_n; i++)
if(n%i==0){
cout << i <<",";
get_divisors(n/i);
return;
}
I'm using visual C++ 2010 and using these libraries...
#include<iostream>
#include<cmath>
It will not let me pass sqrt an integer. It's telling me that it has to be double, float, or something like that. I changed root_n to n/2 and it runs fine... But this isn't what I need!! HELP!!!
Thanks guys and awesome to be here. Court SC,USA
Gotcha, thank you. I looked that up and it worked. However, I am surprised that that is written like that in this book. I tried to be careful about what book I selected to learn from and I guess I struck out! That function was from the text Verbatim! The book was copy write 2010... Did the previous version compiler select the type for you or is this guy a quack? Confused...
It wouldn't be the first error in a programming book.
C++03's <cmath> header has sqrt overloads for float/double/long double, but it doesn't have one for int. Therefore if you give it an int it doesn't know which version to use.
C's <math.h> header only has one version of sqrt which takes a ?double? so if you give it an int, it will automatically cast it to a double without a problem.
I think C++11's <cmath> header added int versions of those functions but I don't know for sure.
Maybe your author got confused between <cmath> and <math.h>. Or maybe he was anticipating C++11. Or maybe he just screwed up.
Dude.....I'm taking this back to Books-a-Million.... It isn't just one time. He does that prime finder over and over to introduce while, for, and so on... EVERY TIME it's the same thing! I mean, I'm not an expert (yet) but a lot of you guys are and I think most would agree that early on is the most important time to avoid bad habits and careless mistakes. Luckily, it's tough to make me give up; but, what about all the people that ran into this and decided that this stuff is just too difficult?? Ya know?
@Court
I looked that up and it worked. However, I am surprised that that is written like that in this book.
It is implementation-defined whether overload C functions will be placed in the global namespace or will not. So the same code can be translated with GCC 4.7.1 and will not be translated with MS VC++ 2010.
It really is implementation specific, and the compiler will spit an error if it doesn't like what you are doing. As long as you can understand the errors, you shouldn't have a problem. It's a lot better than not understanding why your program is giving the wrong output.
As long as you have a different compiler, or even different version of the same compiler than the author, every book is going to have small things like this. Just ask whenever you come across something new that you don't understand.