[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Inline functions not linking successfully

Posted on 2005-04-12
3
Medium Priority
?
272 Views
Last Modified: 2010-04-01
I am writing the following code in MS Visual C++ version 6.0:

class PA_node {
private:
     bool conflict_removed;
public:
     bool conflict_already_removed() const;
};


And the implementation is (it is of course not placed in the same .h file as the class declaration but in a separate .cpp file):

inline bool PA_node::conflict_already_removed() const {
      return conflict_removed;
}



Please note the "inline" prefix in the function defintion. Now my problem is that when I try to link this code the linker complains that it can't find the function definition:

error LNK2001: unresolved external symbol "public: bool __thiscall PA_node::conflict_already_removed(void)const " (?conflict_already_removed@PA_node@@QBE_NXZ)


What puzzles me is that the code looks perfectly okay and also, when I remove the "inline" prefix it links without hitch. Also, the linker is linking some other "inlined" functions without problem and it is complaining about others and to me this sounds very random. What is going wrong here? Or is it just a bug in MS Visual C++.

Please help
0
Comment
Question by:gurry_uor
  • 2
3 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 500 total points
ID: 13760215
Put inline function to h-file. Suppose that C++ compiler compiles some file (let's sey client.cpp) which calls PA_node::conflict_already_removed function. Since this function is inline, this call must be replaced with inline implementation. But compiler doesn't see this implementation because it is in another cpp file. In this case compiler defines this call as external link. This link cannot be resolved because function is defined as inline in another cpp file.
Notice that compiler doesn't care whether inline function is defined in h or cpp file - this is precompiler work.
0
 

Author Comment

by:gurry_uor
ID: 13760247
I tried that and it works. But I still, some other member functions of the same class which are placed in separate .cpp file and are "inlined" are linking perfectly. Only this particular one wasn't. What does that mean? Is it that the compiler is not actually inlining the other functions (since the compiler has the option of ignoring the 'inline' keyword)?

Secondly, if one is always required to place 'inlined' functions in the header file itself, most people would never use the 'inline' keyword because this will force them to place their member function implementations in header file and that would mean they are revealing their implementation to everyone, which most people don't want. So how does it work?

Thanks for your answer.
0
 

Author Comment

by:gurry_uor
ID: 13760258
Oh I get it. The other functions which are linking succefully haven't been called anywhere yet. I am sorry for my stupid second question. So thanks Alex. I accept your answer.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

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…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

834 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