Thread: Including header files

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    80

    Including header files

    Hi all,

    This may seem like an easy question to many of you, but this is troubling me a little. How do u include a header file. Can anyone spell it out like i have never seen c++ before. The books i have dont say much about it.

    heres what i have made in dev C++

    heres sample.h
    Code:
    #include<iostream>
    using namespace std;
    
    void sample(){
    
    cout << "help" << endl;
    
    }
    and here i would assume is the implementation file trial.cpp

    Code:
    #include<iostream>
    
    using namespace std;
    #include "sample.h"
    
    int main() 
    {
        
        sample();
        
        
        system("Pause");
        
        return 0;
        
    }
    i get an error that says main is being redifined and its being defined here, but in the sample.h its not there.

    also is there a certain location that need to have the files for this to work?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Are you sure that's the only sample.h you have?

    Also, the code should be in sample.cpp, and the header file should just have
    void sample( );

    Header files are like the index of a book. They tell you what's available.

    Then you have main.cpp and sample.cpp as files in the project.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    80
    Thanks a lot
    I got it to work, but its a little unclear to me still. Here is the code that I changed to look like what you mentioned earlier

    Sample.h

    Code:
    void sample();
    sample.cpp
    Code:
    #include<iostream>
    #include<sample.h>
    
    using namespace std;
    void sample(){
    
    cout << "help" << endl;
    
    }
    trial.cpp
    Code:
    #include<iostream>
    
    using namespace std;
    #include "sample.cpp"
    
    int main() 
    {
        
        sample();
        
        
        system("Pause");
        
        return 0;
        
    }
    And one list thing. in order to include a file does it have to be after the namespace std; ? because when i put it in before it gives errors.
    So this follows the spltting of files into 3 separate ones. implement, the code, and i forget the last one.
    Last edited by Emeighty; 08-09-2008 at 08:26 AM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > #include "sample.cpp"
    No, this is sample.h as well.

    Then you add sample.cpp to the project.
    Project->settings->source files.... (kinda thing)

    If it were at the command line, it would be
    g++ main.cpp sample.cpp
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jul 2008
    Posts
    80
    ahh, this was silly,

    I had to create a project in dev C++, then i added trial.cpp and added sample.h to the project .

    this it what i got.

    trial.cpp

    Code:
    #include<iostream>
    
    using namespace std;
    #include "sample.h"
    
    int main() 
    {
        
        sample();
        
        
        system("Pause");
        
        return 0;
        
    }
    Code:
    #include<iostream>
    
    
    using namespace std;
    void sample(){
    
    cout << "help" << endl;
    
    }

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Another thing to note (although in this simple example it doesn't matter), you should never put using statements in header files or before an #include statement; otherwise you could be changing the meaning of some of the code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C Header Files
    By devarishi in forum C Programming
    Replies: 8
    Last Post: 12-10-2008, 04:53 PM
  2. Header files in .h
    By swgh in forum C++ Programming
    Replies: 5
    Last Post: 05-29-2008, 08:58 AM
  3. Static variables and header files
    By drrngrvy in forum C++ Programming
    Replies: 8
    Last Post: 12-02-2006, 01:27 PM
  4. Class files including other classes
    By combatdave in forum C++ Programming
    Replies: 7
    Last Post: 11-04-2006, 12:37 AM
  5. more header files
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 10-28-2001, 01:56 PM