Thread: about #include files and header files

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    19

    about #include files and header files

    I am wanting to generate a random number from inside a member function of a class thats located in its header file.

    My question is..where do I put the #include <time.h> and the needed variables to use it (by it I mean the variables the tutorial thats on this site about 'generating random numbers' uses)?

    Do I put #include <time.h>, <cstdlib>, time_t seconds,time(&seconds) and srand((unsigned int) seconds) in the header file or do I put them in the main.cpp?

  2. #2
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    If my class needs a certain header, I include that header in the class's header file. It makes the code feel more complete.
    What is C++?

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The headers must appear in the file where the code calling rand() and srand() and time() will be.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> The headers must appear in the file where the code calling rand() and srand() and time() will be.

    I prefer this solution over the one Vicious proposes. In most cases, you'll be calling those functions in the cpp file, so #include the headers for those functions in your cpp file. Adding the #includes to your header just forces people using your class to #include them as well, which defeats part of the purpose of separating header and source files.

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Daved View Post
    >> The headers must appear in the file where the code calling rand() and srand() and time() will be.

    I prefer this solution over the one Vicious proposes. In most cases, you'll be calling those functions in the cpp file, so #include the headers for those functions in your cpp file. Adding the #includes to your header just forces people using your class to #include them as well, which defeats part of the purpose of separating header and source files.
    Yes, this goes along the same lines as encapsulation & scope. If a certain piece of code doesn't need access to other code, then don't give it access.

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    19
    do I have to put

    time_t seconds;
    time(&seconds);
    srand((unsigned int) seconds);

    in each member function of my class that uses rand()?

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    No. That code should be run only once for the entire time your program is running. Some people do it at the beginning of main, but you can place wherever is appropriate to ensure it is called once before you ever call rand().

  8. #8
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Just to be sure I even understand what I am talking about:

    If you need to declare a time_t variable in the header including the time header before the class header in your cpp file will work? I suppose I should try this again, as my only memories of this is the compiler complaining about "time_t" not being a type.

    Sorry about that mislead!
    What is C++?

  9. #9
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Vicious View Post
    Just to be sure I even understand what I am talking about:

    If you need to declare a time_t variable in the header including the time header before the class header in your cpp file will work? I suppose I should try this again, as my only memories of this is the compiler complaining about "time_t" not being a type.

    Sorry about that mislead!
    If you need time_t in your header, include <ctime> in your header because anyone that uses your header will need to include <ctime> anyways, otherwise if you don't need any types or functions from <ctime> in your header, just include it in your .cpp file.
    You should never organize your code so that it depends on the order of #include statements...

  10. #10
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Ok, that's what I was thinking when I made my original post. I was just making sure I wasn't totally off.
    What is C++?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. #include header files or .cpp files?
    By DoctorX in forum C++ Programming
    Replies: 3
    Last Post: 12-23-2006, 12:21 PM
  2. Header file include order
    By cunnus88 in forum C++ Programming
    Replies: 6
    Last Post: 05-17-2006, 03:22 PM
  3. include library header in header files
    By Raison in forum C++ Programming
    Replies: 6
    Last Post: 09-27-2004, 02:50 AM
  4. Headers that use each other
    By nickname_changed in forum C++ Programming
    Replies: 7
    Last Post: 10-03-2003, 04:25 AM
  5. Using c++ standards
    By subdene in forum C++ Programming
    Replies: 4
    Last Post: 06-06-2002, 09:15 AM