• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 373
  • Last Modified:

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
0
dennis_george
Asked:
dennis_george
5 Solutions
 
rstaveleyCommented:
If you implement the show function into show.cpp it cannot be expanded inline in main.cpp. You need to implement the inline function in your header.
0
 
dennis_georgeAuthor Commented:

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
0
 
mrwad99Commented:
Dennis,

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

Yes.  The point of an inline function is so that at run time the instruction pointer will not have to be offset to the location at which the code body resides.  It is where it is defined, hence execution overall is faster.

>> 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...
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

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

When the compiler compiles an inline function, it actually puts the inline function code where ever you call it.

With a normal functionnn, the compiler only put a jump call where ever you call the function, but with an inline function, the compiler put the actual inline function code where ever it is called.

So if you have the following code:
CTest ob ;
ob.show();

The compiler will compile it like this if show is a real inline function:
CTest ob ;
cout <<"From inline function" << endl ;

This is why it's called an IN-LINE function.  The contents of the function is placed in-line where it is called.

Where as a none inline function would look more like this:
CTest ob ;
jmp show;


The only practical way for a compiler to put contents of a inline function In-Line, is for the function implementation to be included in the header.
0
 
itsmeandnobodyelseCommented:
>> So this means every inline function should be defined in the header file !!! am I correct ???

Some people, e. g. Microsoft, put their inline code to .inl files, mostly uncommented and in ugly presentation (to make it more difficult to steal implementation code, i suppose). However, these .inl files are included in the standard header files.

Regards, Alex
 
0
 
guntherothkCommented:
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 ;
        }
};

0
 
waysideCommented:
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.
0
 
jhshuklaCommented:
>> You cannot guarantee that the compiler will actually inline a function, you cannot force ....
MSVC has __forceinline and gcc has __inline__ to force the compiler.

an alternative and ugly way to do is to #define the function. it works (theoretically) for all compilers because a pre-processor is supposed to replace the textual code and then the highlevel2lowlevel translator come into play.
#define foo()                 \
{                                  \
  //do your thing here     \
}

I suppose i won't work for classes and member functions.
0
 
waysideCommented:
> 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...

0
 
AxterCommented:
>>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.
0
 
AxterCommented:
FYI:
inline functions are also harder to debug, which is another good reason not to inline unless you're sure you need it.
0
 
HendrikTYRCommented:
>> >> 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.
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now