Solved

inline problem

Posted on 2004-10-22
365 Views
Last Modified: 2010-04-02
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
Question by:dennis_george
    12 Comments
     
    LVL 17

    Accepted Solution

    by:
    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
     
    LVL 5

    Author Comment

    by:dennis_george

    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
     
    LVL 19

    Assisted Solution

    by:mrwad99
    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
     
    LVL 30

    Assisted Solution

    by:Axter
    >> 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
     
    LVL 39

    Assisted Solution

    by:itsmeandnobodyelse
    >> 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
     
    LVL 2

    Expert Comment

    by: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 ;
            }
    };

    0
     
    LVL 14

    Expert Comment

    by:wayside
    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
     
    LVL 9

    Assisted Solution

    by:jhshukla
    >> 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
     
    LVL 14

    Expert Comment

    by:wayside
    > 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
     
    LVL 30

    Expert Comment

    by:Axter
    >>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
     
    LVL 30

    Expert Comment

    by:Axter
    FYI:
    inline functions are also harder to debug, which is another good reason not to inline unless you're sure you need it.
    0
     
    LVL 3

    Expert Comment

    by:HendrikTYR
    >> >> 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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
    C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
    The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
    The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

    913 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now