11

I have three 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 to compile them using:

g++ cpp Joy.cpp

g++ say:

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

Who can tell me why...

How to solve this problem if I don't want to define that test() function in the .h file and still want it to be an inline function?

Yishu Fang
  • 9,448
  • 21
  • 65
  • 102
  • 1
    You should put the `inline` functions in header files. You could also be interested by link time optimization (`g++ -flto` with a very recent GCC). – Basile Starynkevitch Mar 10 '12 at 17:44
  • 2
    possible duplicate of [Must the definition of a C++ inline functions be in the same file?](http://stackoverflow.com/questions/9338152/must-the-definition-of-a-c-inline-functions-be-in-the-same-file) – kev Mar 10 '12 at 17:47
  • @kev, Oh, sorry~~ I am new here, and I am a beginner in c++, thank you. – Yishu Fang Mar 10 '12 at 17:54

5 Answers5

12

when you define an inline member function, you should prepend the member function's definition with the keyword inline, and you put the definition into a header file.

When you declare a function inline basically You are telling the compiler to (if possible)replace the code for calling the function with the contents of the function wherever the function is called. The idea is that the function body is is probably small and calling the function is more overhead than the body of the function itself.

To be able to do this the compiler needs to see the definition while compiling the code which calls the function this essentially means that the definition has to reside in the header because the code which calls the function only has access to the header file.

Good Read:
[9.7] How do you tell the compiler to make a member function inline?

Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • I am getting `You do not have permission to access the requested file on this server.` error while visiting the site. Can you help me out by some another link. – Rakholiya Jenish Jun 26 '15 at 20:42
4

The compiler needs the complete definition of the function so that it could be inlined where it is called from. That is possible only if you define it in the header itself.

How does inline function work?

Say, you define this:

 inline void increment(int &i) { ++i; }

and then use it as:

int i = 0;
while( i < N )
{
    std::cout << i << std::endl;
    increment(i);
}

then the compiler translates this code into this (roughly speaking):

int i = 0;
while( i < N )
{
    std::cout << i << std::endl;
    ++i; //replaced the call with the equivalent code which the function 
         //actually executes to produce the same effect
         //(edit typo) it replaces to ++i and not i++ as it was the original.
}

Such replacement of function-call with the code of function itself is said to be inlined. You can say, the function is inlined.

Note that the inline keyword is just a hint for the compiler : it tells the compiler if possible inline me. It is not guaranteed that every inline function call will be inlined by the compiler.

StormByte
  • 1,266
  • 1
  • 13
  • 33
Nawaz
  • 353,942
  • 115
  • 666
  • 851
4

From the standard (N3242, 7.1.2.4):

An inline function shall be defined in every translation unit in which it is used and shall have exactly the same definition in every case.

Have a look here as well: How do you tell the compiler to make a member function inline?

Bojan Komazec
  • 9,216
  • 2
  • 41
  • 51
  • The standard is ISO/IEC 14882:2011. The wording should be "... in which it is _odr-used_...", but otherwise: +1. – CB Bailey Mar 10 '12 at 18:08
  • @Charles You're right. I cited the last working draft (I think it was the last), dated 2011-02-28. – Bojan Komazec Mar 10 '12 at 18:38
  • @CharlesBailey: Could you tell me from where can i get the c++11 (ISO/IEC 14882:2011) standard? thank you ~~ – Yishu Fang Mar 11 '12 at 09:44
  • @UniMouS: http://www.iso.org/iso/iso_catalogue/catalogue_tc/catalogue_detail.htm?csnumber=50372, and see [here](http://stackoverflow.com/questions/81656/where-do-i-find-the-current-c-or-c-standard-documents) – CB Bailey Mar 11 '12 at 10:13
1

Because of the way C++ is compiled in to separate compilation unit (each cpp file typically), the compilation of one cpp file know not of the implmentation of the inlined function in another compilation unit so can't inline it.

The solution is to put the implementation of the inlined function in the header file this way all files using the head have access to the implementation,

111111
  • 15,686
  • 6
  • 47
  • 62
0

an inline function is a function upon which the compiler has been requested to perform inline expansion.

Hence, the whole point of an inline function is that it is implemented in line. There isn't any way to define it in another source file if you still want it to be an inline function.

ApprenticeHacker
  • 21,351
  • 27
  • 103
  • 153