Thread: Classes in header files - compile-time errors.

  1. #16
    Registered User
    Join Date
    Aug 2008
    Posts
    33
    Quote Originally Posted by Daved View Post
    No, not quite right unfortunately.

    You really should never #include a cpp file (there are some advanced cases where you might, but don't worry about those now).

    The solution is to #include "Person.h" in both cpp files that use the Person class. Then, add both cpp files to your makefile or command line. You need to compile both, then link them together.

    BTW, as laserlight suggested earlier, it's not good practice to put the using namespace std in your header file like that. It's not a big deal in small projects, it's just something you should know.
    Okay, so instead of using namespace std, I should use std:string for string types? Also, to avoid including the person.cpp in my driver, I compile person.cpp, then compile the driver, then somehow link the two using the compiler? Sorry just going over your post to get a better understanding.
    Cheers.

  2. #17
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by kbro3
    Okay, so instead of using namespace std, I should use std:string for string types?
    Yes.

    Quote Originally Posted by kbro3
    Also, to avoid including the person.cpp in my driver, I compile person.cpp, then compile the driver, then somehow link the two using the compiler?
    Yes (or rather, the compiler would invoke the appropriate tools, e.g., the linker, to link them for you).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #18
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> 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.

  4. #19
    Registered User
    Join Date
    Aug 2008
    Posts
    33
    Quote Originally Posted by Daved View Post
    >> 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.
    Ah great, yes I thought that it was strange to include the .cpp instead of .h, seeing as one of the points of header files is to "hide" the implementation of classes while allowing their use.
    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!

  5. #20
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #21
    Registered User
    Join Date
    Aug 2008
    Posts
    33
    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)

    Cheers.

  7. #22
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by kbro3 View Post
    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.
    Regardless of headers including headers, wherever you use string, include <string>. It doesn't hurt to include the same header twice or more, and only serves to make your code less error-prone.

    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 ?
    I assume you have something like this?
    Person.h:
    #include <string>
    //...
    std::string my_str;

    Person.cpp:
    #include "Person.h"
    string my_str2;

    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?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #23
    Registered User
    Join Date
    Aug 2008
    Posts
    33
    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!

  9. #24
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I'd say overhead is like 0.1%, because headers include header guards, making sure their contents is only parsed once.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Conflicting Header Files
    By X PaYnE X in forum Windows Programming
    Replies: 17
    Last Post: 01-08-2004, 11:28 AM
  2. #define Header files. End Of File Errors.
    By bartybasher in forum C Programming
    Replies: 8
    Last Post: 07-28-2003, 03:03 PM
  3. Using c++ standards
    By subdene in forum C++ Programming
    Replies: 4
    Last Post: 06-06-2002, 09:15 AM
  4. errors reading header files( i believe)
    By knights32ball in forum C++ Programming
    Replies: 0
    Last Post: 03-19-2002, 04:41 AM
  5. doin' classes in header files?
    By face_master in forum C++ Programming
    Replies: 9
    Last Post: 11-14-2001, 03:56 AM