33

I am trying to use inline member functions of a particular class. For example the function declaration and implementation without inlining is as such:

in the header file:

int GetTplLSize();

in the .cpp file:

int NeedleUSsim::GetTplLSize()
{
    return sampleDim[1];
}

For some reason if I put the "inline" keyword in either one of the implementation and declaration, as well as in both places, I get linker errors as shown:

 Creating library C:\DOCUME~1\STANLEY\LOCALS~1\TEMP\MEX_HN~1\templib.x and object C:\DOCUME~1\STANLEY\LOCALS~1\TEMP\MEX_HN~1\templib.exp 
mexfunction.obj : error LNK2019: unresolved external symbol "public: int __thiscall NeedleUSsim::GetTplLSize(void)" (?GetTplLSize@NeedleUSsim@@QAEHXZ) referenced in function _mexFunction 
mexfunction.mexw32 : fatal error LNK1120: 1 unresolved externals 

  C:\PROGRA~1\MATLAB\R2008B\BIN\MEX.PL: Error: Link of 'mexfunction.mexw32' failed. 

What needs to be in order to get rid of this error (i.e. what am I doing wrong in terms of making these inline member functions)?

stanigator
  • 10,768
  • 34
  • 94
  • 129

5 Answers5

36

You need to put function definition into the header then. The simplest way to hint the compiler to inline is to include method body in the class declaration like:


class NeedleUSsim
{
  // ...
  int GetTplLSize() const { return sampleDim[1]; }
  // ...
};

or, if you insist on separate declaration and definition:


class NeedleUSsim
{
  // ...
  int GetTplLSize() const;
  // ...
};

inline int NeedleUSsim::GetTplLSize() const
{ return sampleDim[1]; }

The definition has to be visible in each translation unit that uses that method.

Nikolai Fetissov
  • 82,306
  • 11
  • 110
  • 171
  • Is this the only way? (I want to separate the declaration and implementation separately for readability reasons) – stanigator Jun 05 '09 at 00:46
  • 1
    Basically yes. The compiler has to know the body of the function while compiling the calls to it, so the body has to be in the included header file. However, you could still just declare the function in the class declaration and add the implementation at a later place in the header file. – Hans Jun 05 '09 at 01:03
  • I see. I guess I would just have to live with writing the code this way then. Thanks for the advices. – stanigator Jun 05 '09 at 02:10
  • 4
    Some people like to put inline functions in a separate header file and include that from the main header file (at the end) instead of having them in the main header. I'm not personally 100% convinced by it (it feels like a waste of time to me) but if you like it, it's another option. – markh44 Jun 05 '09 at 07:19
  • 1
    Yes, I didn't want to mention that .inl pathetic technique :) – Nikolai Fetissov Jun 05 '09 at 14:09
  • Only the top method is working for me in XCode 4.2. (only without const because I am trying to create a setter).If I try the second method it does not work – Miek Mar 12 '12 at 17:06
  • Or place the definition inside the class, then the `inline` keyword is not needed because the function is automatically inlined. – jaques-sam Apr 17 '19 at 09:04
28

from C++ FAQ Lite

If you put the inline function's definition into a .cpp file, and if it is called from some other .cpp file, you'll get an "unresolved external" error from the linker.

How do you tell the compiler to make a member function inline?

agold
  • 6,140
  • 9
  • 38
  • 54
young
  • 2,163
  • 12
  • 19
  • 2
    unfortunately, as I just found out the hard way, this is NOT the case in MSVC 2013 and 2015. I had an inline in a CPP file (in a library), and its code was called from some totally different CPP source code that has an own inline with the same name (but slightly different code, so the result did break the tests). I consider that a bug in that development environment. Hard to find... – chksr Nov 15 '17 at 14:05
5

As others have already pointed out, you need to move the definition of the inlined function to the header file, like so:

class NeedleUSsim
{
  // ...
  inline int GetTplLSize() { return sampleDim[1]; }
  // ...
};

The reason for this is that the compiler needs to know what code to inline when it sees a call to the inlined function. If you leave the definition of the function in the .cpp file for the NeedleUSsim class, the code that the compiler generates for it becomes trapped in the the NeedleUSsim object file. As the compiler only reads source code—it never peeks into another class's object file—it simply has no way to know with what code to replace a call when it's compiling another .cpp file.

Ken Dyck
  • 156
  • 3
3

If you have an inline function you should put the definition in the header file.

Stephen Nutt
  • 3,258
  • 1
  • 21
  • 21
  • I tried putting the inline keyword on the definitions in the header file and I still get the same linker error. – stanigator Jun 05 '09 at 00:12
  • 2
    Not just the inline keyword, the whole definition should go in the header file. So, move it from your .cpp file into your .h file. – ChrisInEdmonton Jun 05 '09 at 00:17
0

See the Inline Guard Macro idiom. This will at least allow you to separate, albeit slightly, the code from the declaration. It also allows you to toggle inlining of functions via a define.

Jamal
  • 763
  • 7
  • 22
  • 32
grepsedawk
  • 5,959
  • 5
  • 24
  • 22