Question:
If Header Files Only Contains Declarations, Where Can We Place the Definitions?!!! C++?
None N
2008-10-05 08:30:35 UTC
How to Link the Definition to the Header File?

Why Definition Can Not Be Done In Header Files?
Four answers:
shane_kerr
2008-10-05 08:55:57 UTC
Generally one places the declarations in the header files, and the definitions in the non-header C++ files (the files ending in .cpp, .cxx, and sometimes .cc or .c++).



So you might have a header file called "example.h" which contains:



class foo {

int bar(void);

};



Then you would have a source file called "example.cxx" which contains:



#include



int foo::bar(void)

{

return 666;

}



Getting another program to use these involves the linker, not the compiler, and that depends on which program you are using (Visual Studio, GNU c++, and so on).
anonymous
2008-10-05 08:53:13 UTC
Defintions can be written in header files. But on including a header file twice in a project, the code is duplicated resulting a linker error.



Header files are used to to relate source files in a project. It contains the declaration of something defined in a source file to be used in another source file.
Bob B
2008-10-05 08:42:18 UTC
There's no such restriction against placing definitions in a C header file.



Is there really one that formally prohibits doing so in C++ header files?



While I've done very little C++, if there is such a restriction, I've never heard of it.
anonymous
2016-12-01 14:08:12 UTC
A header report is a report which proclaims applications which would be used in this technique and can be known yet are extrinsic to the language. Or as one in all my instructors positioned it, "in case you only use intrinsic applications, C can not do very lots. No enter output like printf or scanf, for example". Header data incorporate declarations macros and variables purely. regularly the programmer has compiled the executable code he or she has then appropriate right into a library archive that's then appropriate to this technique via fact the final step in compilation. The syntax is properly worth discussing in any clarification: #comprise ability that it rather is a known header and the preprocessor will seem for it in the known /comprise itemizing (that could regularly get replaced). #comprise "myheader.h" ability it rather is a custom header and the preprocessor will seem for it in the comparable itemizing via fact the report it rather is preprocessing. syntax: it rather is undemanding of direction for initiatives to grow to be dissimilar data. This will advance the prospect that a header report might properly be #blanketed dissimilar circumstances. once you define a function or variable dissimilar circumstances in the comparable scope the compiler throws an errors. to guard in comparison maximum header data are meant to wrap themselves into an if fact. So for myheader.h it might start up: #ifndef MYHEADER #define MYHEADER and after all the declarations and macros: #endif this might reason the preprocessor to seem for a variable stated as MYHEADER. If it would not discover it, because it won't the 1st time, it rather is going to set aside memory for all the declarations and macros. If it does discover it nevertheless, because it rather is going to if an earlier report #blanketed it, it rather is going to bypass to the endif and flow directly to the subsequent report. that's what a header report is.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...