I have a header file, called Person.h:
class Person
{
public:
Person();
private:
std::string name;
};
The implementation is in the file called Person.cpp:
#include <string>
#include "Person.h"
Person::Person()
{
name = "foo";
}
I'm used to avoid nested include files so I'm #include
-ing the <string>
header before including the class header in the class source file.
Now this doesn't compile with the error that std::string
is not a defined name. But if I include in the header file it compiles.
On the web I found that including headers, paying attention to the order of inclusion (which is what I regularly do in C, my primary language) indeed should work (include files are not compiled by themselves but just inserted in the source file).
So my question is, why doesn't it compile without including the string header in the header file? Because the header file is "copy-pasted" into the source file after #include <string>
, I expect it to compile just fine.