#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    17
    Rep Power
    0

    How do you create a header file?


    (In Visual Studio 2010)

    I would click Add new item, Header File, name it, and it would place in Header files. But when I write out code for it, save it, and then call it in my main file it would say "Cannot open source file "Cclasses.h"

    I don't understand what I'm doing wrong. Where am I supposed to place this?
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    3
    Rep Power
    0
    Please show me the code for the header.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    17
    Rep Power
    0
    Sure thing!

    Code:
    int add(int i,int ii)
    {
    	return i + ii;
    }
    I just did a simple adding function to make sure it'll work.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    17
    Rep Power
    0
    Wait, nevermind. I found out how to make it work. Thanks for the help guys!
  8. #5
  9. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,161
    Rep Power
    2222
    Excuse me, but did you put live code in a header file? Why? Never do that! Code belongs in source files, not in header files!

    The purpose of header files is to share common macros (#define's) and data type definitions (including struct declarations and typedefs) among source files in your project, as well as to notify all interested source files, via function prototypes and extern declarations, of functions and global variables that exist in one source file but intended to be used by other source files. Therefore, you only put information about functions and variables in a header file, not the actual functions and global variables themselves.

    Why is that? Take your misbegotten header file containing the actual function, add(), and #include it in two source files in your project. When you try to build that project, you will get a linker error because there will exist two different add functions by the same name -- that is a name collision. Rather, place in one and only one source file the actual function and then in the header file you place the function prototype for add:
    int add(int i,int ii);
    Please note the semicolon, which is what makes that a function prototype. That function prototype can be #include'd in as many source files as pleases you. What it tells the compiler for each source file is that somewhere there exists a function by this description and that the linker will fill in its actual address later.

    Some misguided teachers and authors, in an attempt to avoid teaching you how to properly create a multiple file project, suggest the quick-and-dirty approach of #including files containing live code into one main source file. Avoid them like the plague! Learn to use either your development environment's project manager (Visual Studio has one) or makefiles.

    When you move on to C++, you will see some apparent violations of the rule to not place code into header files; eg, inline code, templates. But those are specific features that do not violate the rule.

    Use header files as they were intended to be used.
  10. #6
  11. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,110
    Rep Power
    1803
    Originally Posted by Apathy
    Please show me the code for the header.
    Why? The content of a file has no bearing on its acessability within a file system :rolleyes: .
  12. #7
  13. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,110
    Rep Power
    1803
    Originally Posted by miz656
    Code:
    int add(int i,int ii)
    {
    	return i + ii;
    }
    That is not what header files are for. Header files should contain declarative code that is defined elsewhere in separate compilation units.

IMN logo majestic logo threadwatch logo seochat tools logo