I am a beginner with C++. When I write the code sometimes I write #include <string>
and the code works, other times I don't write #include <string>
and the code doesn't work. But sometimes it works without #include <string>
.
So do I have to write #include <string>
so that the code works?
Lying by omission includes the failure to correct pre-existing misconceptions. For example, when the seller of a car declares it has been serviced regularly, but does not mention that a fault was reported during the last service, the seller lies by omission.
Lies of omission involve the intentional exclusion of important information, whereas lies of commission involve the intentional generation of false information.
Not all lies involve saying things that aren't true. Sometimes you might omit specific details to avoid an unpleasant reaction or to spare someone's feelings. And you might have wondered, “Is omitting considered lying?” The short answer is yes.
Key points. Telling partial truths, i.e., lying by omission, is an effective method of lying. Omissions, whether accidental or intentional, can change memories. Omissions can lead people to remember the true parts they heard while erasing the parts that were omitted.
If you use members that are declared inside the standard header string
then yes, you have to include that header either directly or indirectly (via other headers).
Some compilers on some platforms may on some time of the month compile even though you failed to include the header. This behaviour is unfortunate, unreliable and does not mean that you shouldn’t include the header.
The reason is simply that you have included other standard headers which also happen to include string
. But as I said, this can in general not be relied on and it may also change very suddenly (when a new version of the compiler is installed, for instance).
Always include all necessary headers. Unfortunately, there does not appear to be a reliable online documentation on which headers need to be included. Consult a book, or the official C++ standard.
For instance, the following code compiles with my compiler (gcc
4.6):
#include <iostream> int main() { std::string str; }
But if I remove the first line, it no longer compiles even though the iostream
header should actually be unrelated.
It is possible that other headers that you do include have #include <string>
in them.
Nonetheless, it is usually a good idea to #include <string>
directly in your code even if not strictly necessary for a successful build, in case these "other" headers change - for example because of a different (or different version of) compiler / standard library implementation, platform or even just a build configuration.
(Of course, this discussion applies to any header, not just <string>
.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With