c++functioninline

Must the definition of a C++ inline functions be in the same file?


I defined a function show() as inlined in a header file called ex.h and the definition of the function inside ex.cpp. I expected that this will give me an error since the compiler will not know what to replace where the show() function is called. But because I'm using an IDE, it worked fine. How could this happen?

And BTW when I tried to compile it manually it gave me an error that the show() is used but not defined.


Solution

  • It's imperative that the function's definition (the part between the {...}) be placed in a header file, unless the function is used only in a single .cpp file.
    In particular, if you put the inline function's definition into a .cpp file and you call it from some other .cpp file, you'll get an "unresolved external" error from the linker.

    [read more]