Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

inline problem

Posted on 2004-10-22
12
Medium Priority
?
372 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
Comment
Question by:dennis_george
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
12 Comments
 
LVL 17

Accepted Solution

by:
rstaveley earned 800 total points
ID: 12379228
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
ID: 12379722

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
mrwad99 earned 400 total points
ID: 12379895
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 30

Assisted Solution

by:Axter
Axter earned 400 total points
ID: 12380337
>> 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
itsmeandnobodyelse earned 200 total points
ID: 12380710
>> 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
ID: 12381530
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
ID: 12384987
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
jhshukla earned 200 total points
ID: 12386610
>> 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
ID: 12386771
> 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
ID: 12387730
>>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
ID: 12387737
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
ID: 12389977
>> >> 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

618 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