Thread: More than one file

  1. #1
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949

    More than one file

    When compiling more than one file at once, do you have to put the include statements in all the files that need them, or just the main? Example: Can you do this:

    Code:
    FILE 1:
    class A {
     public:
      int a;
      A() {
       a = sqrt(3555);
      }
    };
    
    FILE 2:
    class B {
     public:
      int b;
      B() {
        b = pow(3344, 4)
      }
    };
    
    FILE 3:
    #include <iostream>
    #include <cmath>
    using namespace std;
    
    int main() {
    A a;
    B b;
    cout << a.a;
    cout << b.b;
    }
    Or would each file have to include <cmath> ? Thanks !
    Do not make direct eye contact with me.

  2. #2
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    Basically if you include libraries in your main.cpp file and underneath that include other .cpp files than you would be OK.

    here is an example:
    Code:
    //MAIN.cpp
    #include <iostream>
    #include <cmath>
    using namespace std;
    
    #include <"file1.cpp">
    #include <"file2.cpp">
    this works fine as long as file1 doesn't call anything from file 2. If using namespace std; would be placed at the very buttom, than you would have to also tyoe it out in both file1 and file2. Also you don't want to include a file or a library more then once so you could use ifndef.

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  3. #3
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    OK, thanks.
    Do not make direct eye contact with me.

  4. #4
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Uhh... don't include source files. It can very drastically increase compile time, and is generally not considered good practice. Just include the headers. The headers should have declarations only (except for templated functions/classes), and the source files should implement them. Then, compile your source file into an object file, and link it in.

    Include headers in any file where they are needed (i.e. wherever functions are used, or classes instantiated from that header). Protect from multiple inclusions by putting an inclusion guard around the header file.

    Code:
    // Inclusion guards
    #ifndef MY_HEADER_H
    #define MY_HEADER_H
    
    // Any includes
    
    // Declarations and template definitions
    
    #endif
    If you just have references to a class in a header or pointers (uninstantiated), then you do not need to include the header, you only need forward declarations. Once you instantiate something (on the stack or with 'new'), you need to include the header.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. Making a LIB file from a DEF file for a DLL
    By JMPACS in forum C++ Programming
    Replies: 0
    Last Post: 08-02-2003, 08:19 PM
  4. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 10:57 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM