Asked • 05/28/19

Why are C++ inline functions in the header?

<sub>NB This is not a question about how to use inline functions or how they work, more why they are done the way they are.</sub>The declaration of a class member function does not need to define a function as `inline`, it is only the actual implementation of the function. For example, in the header file:<!-- language: lang-cpp --> struct foo{ void bar(); // no need to define this as inline }So why does the inline implementation of a classes function *have* to be in the header file? Why can't I put the inline function the `.cpp` file? If I where to try to put the inline definition in the `.cpp` file I would get an error along the lines of:<!-- language: none --> error LNK2019: unresolved external symbol "public: void __thiscall foo::bar(void)" (?bar@foo@@QAEXXZ) referenced in function _main 1>C:\\Users\\Me\\Documents\\Visual Studio 2012\\Projects\\inline\\Debug\\inline.exe : fatal error LNK1120: 1 unresolved externals

1 Expert Answer

By:

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.