Link to home
Start Free TrialLog in
Avatar of dennis_george
dennis_georgeFlag for India

asked on

inline problem

Hi all,

Let me explain you my problem.....

I am working on three files : main.cpp, show.cpp, show.h

in show.h I declared a class with a inline function show....

class CTest {
   public :
        .....
        inline int show() ;
};

in show.cpp I defined the inline function....

int CTest::show()
{
   cout <<"From inline function" << endl ;
  return 0 ;
}

and in main.cpp I included show.h, then created an object of class CTest then called the inline function
int main()
{
  ....
  CTest ob ;

  ob.show();
 ...
}

But its giving me compile time error saying
>> undefined reference to `CTest::show()'

I think the problem is that while compiling main.cpp the compiler is unable to find the definition of show function... as it is defined in .cpp file.... and we are including .h file in main.cpp....

So my question is that
* Did I correctly caught the problem or not ?
* If yes then what is the way to remove the error and use my inline function.
* If not then what  is the error

Thanks in advance
Dennis
ASKER CERTIFIED SOLUTION
Avatar of rstaveley
rstaveley
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dennis_george

ASKER


So this means every inline function should be defined in the header file !!! am I correct ???

And there is no way to define a inline function inside a cpp file ....ofcoz without including the cpp file....

Dennis
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of Axter
Axter
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of guntherothk
guntherothk

What you wanted to do was...

// Show.h
class CTest {
   public :
        .....
        int show() ;  // notice no inline here
};

inline int CTest::show() // inline here
{
   cout <<"From inline function" << endl ;
  return 0 ;
}

// main.cppint main()
{
  ....
  CTest ob ;

  ob.show(); // invoke here, needs to see body of function here
}

As everybody else has pointed out, the body of the inlined function has to be available to the compiler at the point of invocation

You can also put the inline function body directly in the class definition as...
// Show.h
class CTest {
   public :
        .....
        int show()   // no inline here, it's implicit when you do it this way
        {
            cout <<"From inline function" << endl ;
            return 0 ;
        }
};

When working with inline functions, it is also important to remember that (at least for Microsoft's compiler) that "inline" is just a suggestion to the compiler.

You cannot guarantee that the compiler will actually inline a function, you cannot force the compiler to inline a particular functon if it doesn't want to, and depending on your compile switches it may inline other functions even if you haven't used the inline keyword.

So I generally don't bother using "inline", the compiler will do what it wants to anyway.

I've never seen anything that outlines the criteria the compiler uses to decide whether or not to inline a function.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
> MSVC has __forceinline and gcc has __inline__ to force the compiler.

Heh.

The msdn page for the compiler option that controls inlining (/Ob) says

"You cannot force the compiler to inline a particular function."

Guess it's wrong...

>>it is also important to remember that (at least for Microsoft's compiler) that "inline" is just a suggestion to the compiler.

This is not just for the MS compiler.  This is for any C++ compliant compiler.

According to C++ standard, inline is a suggestion to the compiler.

That's a C++ feature, and not a MS feature.


>>So I generally don't bother using "inline", the compiler will do what it wants to anyway.

You should only inline if you're sure you need it for peformance.
However, I generally do inline functions that return member data.

inline const std::string& GetFileName()const {return m_FileName;}

For such basic simple requirements, it's more efficient, and much easier to code a one line inline function, then to create a seperate function in the *.cpp file.
Anything more complicated then that, should be put in *.cpp file, and should only be inline if you're really sure you need it due to performance requirement.
FYI:
inline functions are also harder to debug, which is another good reason not to inline unless you're sure you need it.
>> >> And there is no way to define a inline function inside a cpp file ....ofcoz without including the cpp file....

>>Why would you want to ?  If the code is defined in the source body, then it is not an inline function...

To hide the implementation from the person who is going to use your library.
If you implement your inline function in the header file, then the user can change that implementation.
You may want to give them a header file and a compiled library (still with inline functions), so then you do it like guntherothk suggested.

inline functions are the correct and safer new form of macros.