>> Sorry just going over your post to get a better understanding.
No need to apologize. I wish more people would do that.
And I think you understood me correctly. I believe if you just compile the two cpp files in the same command line it should be able to compile them both and link them. Unfortunately I don't remember the exact command line options off the top of my head.
And of course I don't expect anyone to look up compiler command line options for me, I'll do that myself! Thanks for putting up with a newbie!
In case you find compiling confusing, there are IDEs out there that does the job for you. Code::Blocks and Visual Studio are two popular choices.
Good luck with exploring C++. It's a big, complex and advanced language, but as a reward, it is very fast and very flexible.
Sorry to bring this up again, just wanted to get everything working the right way, not interested in bad code to make things work.
I checked command line options, and found how to "link" the Person.cpp file to my driver:
(i'm using g++ on Linux)
g++ -o mainperson mainperson.cpp Person.cpp
^mainperson is the name of the executable I want, mainperson.cpp is the driver and Person.cpp is the file that hold the definitions of Person class which is declared in Person.h (Person.cpp #includes Person.h)
Now that works, and everything in my program works as intended.
I also noticed that since I #include<string> in my Person.h, I don't need to #include<string> in my mainperson.cpp, since mainperson.cpp includes Person.h, is that right? That is, i'm able to use string without problem in my driver because my driver includes a header file which itself includes string. <- I apologise for the simplicity of this question, but we sort of get code thrown at us without much explanation, and i'd like to know the workings behind it.
My second question is that I get compile errors when I remove using namespace std from Person.h and replace with std::string for individual string. What am I missing? Do I need to also put std::string in the corresponding Person.cpp, not just in Person.h ?
(sorry for the long post)
I assume you have something like this?My second question is that I get compile errors when I remove using namespace std from Person.h and replace with std::string for individual string. What am I missing? Do I need to also put std::string in the corresponding Person.cpp, not just in Person.h ?
If that is the case, then you must understand what the use of using namespace std does and what it affects.
string is located inside a namespace called std. Thus to use string, you must type its namespace first: std::string. However, there is also something called the global namespace. The global namespace does not need any prefix to use. When you use using namespace std, you are important everything from the std namespace into the global namespace.
Also know that there is no magic about #include. It simply puts the contents of the file you are including and replaces the #include line with that contents.
So before when you have using namespace std in the header, it also got placed and affected the whole .cpp file, as well (hence why it's bad to put using directives in headers).
Now that you haven't done this, you will have to put a new using namespace std into your .cpp file or prefix std::. Make sense?
Ah yes, got it. So there are no, or at least minimal performance overheads in having several includes?
Thanks for taking the time to explain the logic behind the code, that's what I was after!