C++ inlining class methods causes undefined reference

23,797

Solution 1

The body of an inline function needs to be in the header so that the compiler can actually substitute it wherever required. See this: How do you tell the compiler to make a member function inline?

Solution 2

7.1.2/4 of the Standard:

An inline function shall be defined in every translation unit in which it is used...

You use TestMethod in main.cpp, but it's not defined there.

... If a function with external linkage is declared inline in one translation unit, it shall be declared inline in all translation units in which it appears; no diagnostic is required.

You define (and hence also declare) TestMethod inline in my_class.cpp, but not in main.cpp.

The fix in this case is to move the function definition to the header file, either like this:

class MyClass {
 public:
  void TestMethod() {}
};

or like this:

class MyClass {
 public:
  inline void TestMethod();
};

inline void MyClass::TestMethod() {}

Solution 3

You've defined it as not inlined in the header file, while in the cpp file you're trying to define it as inline. That's a conflicted definition and it won't be able to find one from the other. Your header is where you really place the inline keyword.

However, I'd remove the inline keyword as it's really more of a suggestion to the compiler anyways. You really only need it when there's a free-floating function in the header and you don't want multiple definitions popping up in your code base.

Share:
23,797
FrankMN
Author by

FrankMN

Updated on July 09, 2022

Comments

  • FrankMN
    FrankMN almost 2 years

    I'm getting a compiler error when I try to inline a method of one of my classes. It works when I take away the "inline" keyword.

    Here's a simplified example:

    main.cpp:

    #include "my_class.h"
    
    int main() {
      MyClass c;
      c.TestMethod();
    
      return 0;
    }
    

    my_class.h:

    class MyClass {
     public:
      void TestMethod();
    };
    

    my_class.cpp:

    #include "my_class.h"
    
    inline void MyClass::TestMethod() {
    }
    

    I try compiling with:

    g++ main.cpp my_class.cpp
    

    I get the error:

    main.cpp:(.text+0xd): undefined reference to `MyClass::TestMethod()'
    

    Everything is fine if I take away the "inline". What's causing this problem? (and how should I inline class methods? Is it possible?)

    Thanks.

  • bdonlan
    bdonlan over 13 years
    inline keywords are most certainly not just a hint, at least in C++ - they tell the compiler to make the definition weak, and to emit it only if needed. This means it must be present where used, but on the other hand may be included more than once
  • FrankMN
    FrankMN over 13 years
    I still get an error no matter where I put the inline (or if I put inline in both). Out of the 4 combinations (inline in my_class.h and/or in my_class.cpp), only the one with no inline anywhere works.
  • VL-80
    VL-80 over 8 years
    The link leads to the page with You do not have permission to access the requested file on this server. error