The special rules for inline functions require that they be defined in each file in which
they are used.The easiest way to make sure that inline definitions are available to all files
in a multifile program is to include the inline definition in the same header file in which
the corresponding class is defined.

please write a sample code to make me understand the above point.

To fully understand this you need to understand how stages of compilation work over several files. However, conceptually, you can think of this using the following example:

File: main.c

int add (int x, int y) {
   return x + y; 

int main () {
   int i = 3, j = 7; 
   int result = add (i, j);
   return result;

Now, when you inline the add function you can think of the the body of the function being placed at the call site instead of being defined separately from the call site (as above). In other words, you get something like:

int main () {
   int i = 3, j = 7;
   int result = 0;
   int tmp = i + j;
   result = tmp;
   return result;

Notice how there is no longer an add function to call; the functionality is now place inline in the calling function.

This is fine if everything is placed in the same compilation unit (i.e. file) but if you are expecting to call add from some other source file (foo.c, for example) but it gets inlined in main.c there is no longer anything to call and the code in foo.c will fail to resolve a symbol.

Now, the way #include works is that is copies code from the included file into the file you are writing. This happens before the inlining phase of compilation. Therefore, if you provide a definition to a function in a header file and include that definition in each source that uses the definition there will never be an failure to resolve an external symbol (because a local definition will be found and inlined).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.