943,846 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 526
  • C++ RSS
Oct 22nd, 2008
0

Inline question

Expand Post »
As I was busy inlining some of my functions, I was wondering if it is really necessary to inline anything. Doesn't the compiler automatically inline stuff, or do I have false memories? And if the compiler does inline stuff for me, would it be best to let it do its job or should I mess up something by doing it myself?

The reason I thought of the first question is because I can't get my now-inlined functions to work. A quick example:

test.cpp
c++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <string>
  3. #include "test.h"
  4.  
  5. void main()
  6. {
  7. std::cout << getString();
  8. std::cin.get();
  9. }

test2.cpp
c++ Syntax (Toggle Plain Text)
  1. #include "test.h"
  2.  
  3. inline
  4. std::string getString()
  5. {
  6. return "Beam me up, Scotty!";
  7. }

test.h
c++ Syntax (Toggle Plain Text)
  1. #ifndef TEST_H
  2. #define TEST_H
  3.  
  4. #include <string>
  5.  
  6. std::string getString();
  7.  
  8. #endif

This generates the error:
C++ Syntax (Toggle Plain Text)
  1. test.obj : error LNK2019: unresolved external symbol "class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __cdecl getString(void)" (?getString@@YA?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ) referenced in function _main
  2. C:\w\e\Projects\test\Debug\test.exe : fatal error LNK1120: 1 unresolved externals

Removing the inline will make the program work.
Similar Threads
Reputation Points: 11
Solved Threads: 5
Light Poster
Evan M is offline Offline
42 posts
since Sep 2007
Oct 22nd, 2008
0

Re: Inline question

Compilers are free to choose to ignore the inline keyword or not.

Move the code in test2.cpp to test2.h since you use the linline keyword, then delete test2.cpp from the project.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,951 posts
since Aug 2005
Oct 22nd, 2008
1

Re: Inline question

The function definition is not being included in your test.cpp module, so the linker can't find it. Often inline function definitions are saved as a different type of file (like test.inl) which is itself #include -ed.

if you really need to inline something and aren't sure whether the compiler will or not, standard practice is to write the function definition in the function declaration. In other words move the code into test.h as follows:

C++ Syntax (Toggle Plain Text)
  1. #ifndef TEST_H
  2. #define TEST_H
  3. #include <string>
  4.  
  5. std::string getString(){ return "Beam me up, Scotty!"; };
  6.  
  7. #endif

or include the code module as well...usually instead of being saved as a .cpp the file is saved as another type (often .inl) which you will also have to #include.

As to whether it's worth doing, that depends. If it is absolutely time-critical code and you are unsure as to whether the compiler will or will not inline functions, and it is worth the code bloat, sure. Otherwise, letting the compiler decide when to inline code...and with compilers you can change how aggressive they are about inlining code, also. I can't really see the need for it in a function like the one you mentioned...it's more often seen when writing very time critical math functions and such.
Reputation Points: 13
Solved Threads: 6
Light Poster
seanhunt is offline Offline
40 posts
since Oct 2008
Oct 22nd, 2008
0

Re: Inline question

Don't forget, main returns int
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Oct 22nd, 2008
0

Re: Inline question

Thanks for the help.
Reputation Points: 11
Solved Threads: 5
Light Poster
Evan M is offline Offline
42 posts
since Sep 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: my program-calculator
Next Thread in C++ Forum Timeline: calculator and loop problem next





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC