Inline functions not linking successfully

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

class PA_node {
     bool conflict_removed;
     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
Question by:gurry_uor
    LVL 48

    Accepted Solution

    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.

    Author Comment

    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.

    Author Comment

    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.

    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.

    Join & Write a Comment

    This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
    C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (…
    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.
    The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

    734 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

    20 Experts available now in Live!

    Get 1:1 Help Now