c++ - Why I can't define inline member function in another file? -


i have 3 files:

1. joy.h

class joy { public:     void test(); }; 

2. joy.cpp

#include "joy.h" inline void joy::test() {} 

3. main.cpp

#include "joy.h"     int main() {     joy r;             r.test();             return 0; } 

i try compile them using:

g++ cpp joy.cpp 

g++ say:

main.cpp:(.text+0x10): undefined reference `joy::test()' 

who can tell me why...

how solve problem if don't want define test() function in .h file , still want inline function?

when define inline member function, should prepend member function's definition keyword inline, , put definition header file.

when declare function inline telling compiler (if possible)replace code calling function contents of function wherever function called. idea function body is small , calling function more overhead body of function itself.

to able compiler needs see definition while compiling code calls function means definition has reside in header because code calls function has access header file.

good read:
[9.7] how tell compiler make member function inline?


Comments

Popular posts from this blog

delphi - How to convert bitmaps to video? -

jasper reports - Fixed header in Excel using JasperReports -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -