My first post here, and I'm sure my question has been asked before, but couldn't quite find it.
In my code for some small app, I can use string, rand(), srand(stuff) and time(0) without any trouble, but the problem is, I apparently don't have to include ctime, cstdlib or cstring...
I do have to include iostream (using namespace std, because I'm lazy ^^), but with that, all is working fine. No compiler error, and the app runs great.
Now, is this bad? It feels wrong. I read (I am learning it by reading, for now) that I have to include these things. Also, I am afraid I won't easily learn what needs to be included by learning from compiler errors, since I don't get any.
I use the Code::Blocks IDE 8.02, which came with a compiler. (I'm on Windows XP, if it matters)
(I tried it at a friend, who has Microsoft Visual Studio 2008, and it didn't work without including ctime.... However, when including ctime it did work, even without having to include cstdlib, and I am really using rand(), which, as I understood, is defined in cstdlib)
I'm not sure, but I'll take my best guess. When you switched compilers, it gave you errors, so my best guess is that the headers you need are being included implicitly by <iostream> (some function in <iostream> uses rand(), so <iostream> also includes <cstdlib>)
this would implementation specific which is why VC++ is giving you an error and GCC(the compiler that Code::Blocks uses) is not.
Again, I'm not sure if this is correct, it's just my best guess.
I would include the headers anyway, they are guarded so you are not bloating your code, and your code becomes a bit more self-documenting.
strange that it works without it, as the mingw compiler that comes with codeblocks doesn't allow this usually.
I have to use #include <string> with codeblocks, and the others you mentioned otherwise I get errors.
But then I'm also using a nightly build 5911 as I like to have auto closing brackets and inverted commas, and other fixes. etc.
My suggestion is always include all the header you know you'll be using, never rely to nested includes as they may change.
To know in which header a symbol is declared check the reference: http://www.cplusplus.com/reference/clibrary/cstdlib/rand/ http://www.cplusplus.com/reference/clibrary/ctime/time/
Is better having some extra lines of code than something which may not compile
Anyway... I did not intend to not include them. That's also my concern. I might forget to include something I should need, and not get an error pointing out my mistake.
I just don't understand why it is working... I will keep trying to get errors :P
Some headers may be including other headers as darkestfright stated before.
The C++ headers usually have the name of the class they contains so they are easy to know when you need them
eg: std::string is in <string>, std::fstream in <fstream> etc.
Some C functions may be weirdly placed ( eg: abs is in <cstdlib> instead of being in <cmath> ) but if you are unsure go to the reference page.
@ Bazzy:
I understand that, but found it hard to imagine they would do that.
Nevertheless, I just got the idea of renaming cstdlib and ctime, such that the compiler can't find them, even if they are included in other header files, and miraculously... I got errors ^^...
Turns out "ctime" is included in "cwchar" and "cstdlib" is included in "stl_algobase.h"...
Here's what I tracked down by looking at some header files: (-> means includes)
[I found these files in the "<stuff>/MinGW/include/c++/3.4.5/" folder, which has a subfolder named "bits"]
Kind of a disappointment, really. I don't want to change the header files, because I don't think that'd be a good idea.
Anyway... Thanks for the help... I'll try to keep track of what needs to be included myself, just to be sure. If there's a way to fix it, I'd of course be glad to hear it, but I'm at least content that I know the source of my problem. I guess I'll mark this as solved.