32
loading...
This website collects cookies to deliver better user experience
In function 'int main()':
error: 'x' was not declared in this scope
x = 2;
^
[ERROR in Compilation]
element
refers to functions, classes, structs, variables etc throughout this article.)elements
repeatedly in our different C++
programs if they're useful in varied cases. This would result in a lot of code repetition throughout our code-base; and making one change in an element
would result in copying the same change over and over again in different files.elements
that are predicted to be of use often before hand and use them later without worrying about their declaration. To keep our code modular, it fits to combine such 'utility' elements
in one single file and keep importing it in other c++
programs. header files
; these are declarations and definitions of various elements
which are ready to be used in a c++
program. Header files have a .h
extension instead of a .cpp
one; this tells everyone that my_file.h
is a header file that contains declarations and definitions of various 'utility' elements
. A header file can than be included in a cpp
program as such :Do not bring complete namespaces
into scope with the using
directive (read more about using
here) as this might conflict with other elements
present in the file where this header is being included
into.
Use const
variable definitions to make sure a program including
this header file cannot change it.
Use inline
function definitions and named namespaces
.
PS: This is an article in my series Quick Introduction to a concept in C++. You can find all the articles in this series here.