March 4th, 2013, 06:33 PM
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?
March 4th, 2013, 06:36 PM
Please show me the code for the header.
March 4th, 2013, 06:45 PM
I just did a simple adding function to make sure it'll work.
int add(int i,int ii)
return i + ii;
March 4th, 2013, 06:53 PM
Wait, nevermind. I found out how to make it work. Thanks for the help guys!
March 4th, 2013, 09:37 PM
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.
March 5th, 2013, 07:39 AM
Why? The content of a file has no bearing on its acessability within a file system :rolleyes: .
Originally Posted by Apathy
March 5th, 2013, 07:42 AM
That is not what header files are for. Header files should contain declarative code that is defined elsewhere in separate compilation units.
Originally Posted by miz656