*.h file, function definitions.. help

This is a discussion on *.h file, function definitions.. help within the C++ Programming forums, part of the General Programming Boards category; I was told by my professor that my function prototypes should be included in my *.h file. But, do I ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    19

    Question *.h file, function definitions.. help

    I was told by my professor that my function prototypes should be included in my *.h file. But, do I also include the function definitions in that file, or do those go in my *.cpp file? Please help..

    Josh Stevanus
    jrstevan@svsu.edu

  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Function definitions go in the .cpp.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    An example of how you potentially could have things set up in a sample project...

    File.H
    Code:
    #ifndef FUNC_H
    #define FUNC_H
    int func(int);
    #endif
    File.Cpp
    Code:
    #include "File.H"
    
    int func(int data)
    {
        ...
    }
    Main.Cpp
    Code:
    #include "File.H"
    
    int main()
    {
        int result, val;
        ...
        result = func(val);
    }
    Thus this sample project has 2 cpp files and 1 header file. You compile the 2 cpp files into object files and its the linkers job to combine those (typically along with some other library code) into a fully functioning executable file.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 09:28 AM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 09:18 PM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 04:44 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 10:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21