title: Mysterious "undefined reference to" error in c++
body: <p>I get this error when running the following code, I can't figure out what is the root cause. Anyone can help me spot it, thanks!</p>
  <pre><code>/usr/bin/ld: /tmp/ccLgxyJl.o: in function `MyFunctionRunner::callFunc()':
  main.cpp:(.text._ZN16MyFunctionRunner8callFuncEv[_ZN16MyFunctionRunner8callFuncEv]+0xb): undefined reference to `MyFunctionRunner::func_'
  </code></pre>
  <pre><code>#include &lt;iostream&gt;
  #include &lt;functional&gt;
  
  using namespace std;
  
  class MyClass1{
      public:
          void printX(){
              std::cout&lt;&lt;&quot;print X from MyClass1&quot;&lt;&lt;std::endl;
          }
      
  };
  
  class MyClass2{
      public:
          void printX(){
              std::cout&lt;&lt;&quot;print X from MyClass2&quot;&lt;&lt;std::endl;
          }
  };
  
  
  class MyFunctionRunner{
    public:
        static void callFunc(){
           func_(); //Calls the function that's set
        }
        static void setFunc(std::function&lt;void()&gt; func){
           func_ = func; //Sets a function
        }
   private:
       static std::function&lt;void()&gt; func_;
   };
   
  
  template&lt;class MyClass&gt;
  void MyFunc(){
     MyClass myObj{};
  
     MyFunctionRunner::setFunc(
       [myObj]() mutable{
          myObj.printX();
     }); //Passes in a lambda function as a parameter that captures `myObj` by value. 
   };
  
   
  int main() {     
      MyFunc&lt;MyClass1&gt;();
      MyFunc&lt;MyClass2&gt;();
      MyFunctionRunner::callFunc();
      return 0;
  }
  </code></pre>