Hi guys,

I was trying to use inline function and i put inline function definition in .cpp file
by using inline keyword like below..

class SomeApp
{
private:
SomeApp()
{
}
public:
 returnType_t SomeState(); 

};

inline returnType_t SomeApp::SomeState()
{
 code lne 1...
} 

I was trying to access SomeState function in another file say fileSubscriber.cpp
My SomeApp class is singleton i didn't put my getInstance code here.

Someapp::GetInstance().SomeState();
It compiles but it gives linking error
fileSubscriber.cpp:136: undefined reference to `SomeApp::SomeState()`

Now if i put my inline function in SomeApp.hpp it does not give linking error. So is something wrong with inline function to be put in .cpp file or i am missing something.

Thanks

If you want a class method to be inline then just code it directly in the class declaration. The inline keyword is unnecessary here.

class SomeApp
{
private:
SomeApp()
{
}
public:
 returnType_t SomeState()
 {
    // code here
      return 0;
 }

};

Yeah i can. But waht if i want to use it outside as i stated above.

What IDE are you using? I use VS 2008 Pro and I get the same thing when I try to inline in the *.cpp file.

I don't think VS can do it. There must be something about how it interprets the inline keyword that messes up the linker.

EDIT:
Did a little looking around on MSDN and I found this:
http://msdn.microsoft.com/en-us/library/z8y1yy88(v=VS.90).aspx

It seems that function inlining only works when certain compiler switches are(n't) specified. Some of those switches appear to be associated with the Debug compilation configuration.

Edited 5 Years Ago by Fbody: n/a

I don't think VS can do it.

No conforming compiler will do it without an extension. Inline definitions must be provided in every translation unit, so it makes far more sense to place the definition in your class definition, or put an external definition in the header. Otherwise you'd need to redefine the member function in every .cpp file.

No conforming compiler will do it without an extension. Inline definitions must be provided in every translation unit, so it makes far more sense to place the definition in your class definition, or put an external definition in the header. Otherwise you'd need to redefine the member function in every .cpp file.

So are you saying then that you would have to do something like this?

//Sample.h
class Sample {
 private:
   int PrivateVal;
 public:
   // ...
   extern int GetPrivateVal();
};
//Sample.cpp
#include "Sample.h"
inline int Sample::GetPrivateVal() {
  return PrivateVal;
}

Edited 5 Years Ago by Fbody: n/a

I'm saying that unless the inline definition is in the header, you would have to do this:

// Sample.cpp
#include "Sample.h"

inline int Sample::GetPrivateVal() {
  return PrivateVal;
}

// ...
// main.cpp
#include "Sample.h"

inline int Sample::GetPrivateVal() {
  return PrivateVal;
}

int main()
{
  // ...
}
// SomeOtherFile.cpp
#include "Sample.h

inline int Sample::GetPrivateVal() {
  return PrivateVal;
}

// ...

In other words, GetPrivateVal has a definition in every single .cpp file in the project.

I see, I was confused because you mentioned you would have to "put an external definition in the header".

Obviously, defining it within the class definition would be ideal, but would something like this still work?

//Sample.h
class Sample {
 private:
   int PrivateVal;
 public:
   // ...
   int GetPrivateVal();
};

inline int Sample::GetPrivateVal() {
  return PrivateVal;
}

I apologize for the hijack-like nature of this, but I see this as a big learning opportunity for both the OP and myself.

Edited 5 Years Ago by Fbody: n/a

Obviously, defining it within the class definition would be ideal, but would something like this still work?

Sure, either way the definition is in the header and ultimately makes it into any translation unit where Sample is used. Which is better depends on the length of GetPrivateVal and personal preference.

Comments
:)
This article has been dead for over six months. Start a new discussion instead.