First of all, the originally shown code would not compile no matter what was #include
d. That's because the class name is std::string
, and not string
. As you've eventually figured out, and updated your question with.
Each class, or template, or any other resource that's defined by the C++ library requires an appropriate standard header file to be #include
d. Although including a particular header file could also, indirectly, include some other ones, so you end up getting those header files' contents "for free", this would be relying on a compiler-specific feature. When I regularly update my code and rebase against a newer version of gcc
I often find some bits, here and there, that are now missing an explicit #include
of the right header file, because, unknowingly, I missed it, but the previous compiler version's header files ended up pulling it in anyway.
So, after fixing the class name to std::string
you may find out that the resulting code does or does not compile. If it does compile, it's only due to the good graces of your specific compiler's headers, and it may not be compilable by other compilers, or by newer versions of the same compiler you are using.
P.S. You should avoid using namespace std;
, this is bad practice. As you've started learning C++, now would be the best opportunity to avoid acquiring bad programming habits.