title: Yet another abstract class issue in C++ with closures
body: <p>I have a problem compiling this code with <code>g++ -std=c++11</code> argument:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;unordered_map&gt;
  #include &lt;functional&gt;
  #include &lt;iostream&gt;
  using namespace std;;
  
  template &lt;typename T&gt; class opt {
      public:
          opt() {};
          ~opt() {};
          virtual int isEmpty() = 0;
          virtual T getObject() = 0;
  };
  
  template &lt;typename T&gt; class oopt : public opt&lt;T&gt; {
      private:
          T ret;
      public:
          oopt(T obj) { ret = obj; };
          ~oopt() override;
          int isEmpty() { return 0; };
          T getObject() { return ret; };
  };
  int main(void) {
  
      function&lt;opt&lt;int&gt;(int)&gt; fu = [](int j) { return (oopt&lt;int&gt;(10)); };
  
      return 1;
  }
  </code></pre>
  
  <p>This code returns the following error:</p>
  
  <pre><code>assoc_array.cc: In instantiation of ‘class oopt&lt;int&gt;’:
  assoc_array.cc:68:63:   required from here
  assoc_array.cc:29:3: error: ‘oopt&lt;T&gt;::~oopt() [with T = int]’ marked override, but does not override
  In file included from assoc_array.cc:3:0:
  /usr/include/c++/4.7/functional: In instantiation of ‘static _Res std::_Function_handler&lt;_Res(_ArgTypes ...), _Functor&gt;::_M_invoke(const std::_Any_data&amp;, _ArgTypes ...) [with _Res = opt&lt;int&gt;; _Functor = main()::&lt;lambda(int)&gt;; _ArgTypes = {int}]’:
  /usr/include/c++/4.7/functional:2298:6:   required from ‘std::function&lt;_Res(_ArgTypes ...)&gt;::function(_Functor, typename std::enable_if&lt;(! std::is_integral&lt;_Functor&gt;::value), std::function&lt;_Res(_ArgTypes ...)&gt;::_Useless&gt;::type) [with _Functor = main()::&lt;lambda(int)&gt;; _Res = opt&lt;int&gt;; _ArgTypes = {int}; typename std::enable_if&lt;(! std::is_integral&lt;_Functor&gt;::value), std::function&lt;_Res(_ArgTypes ...)&gt;::_Useless&gt;::type = std::function&lt;opt&lt;int&gt;(int)&gt;::_Useless]’
  assoc_array.cc:68:67:   required from here
  /usr/include/c++/4.7/functional:1909:7: error: invalid abstract return type for function ‘static _Res std::_Function_handler&lt;_Res(_ArgTypes ...), _Functor&gt;::_M_invoke(const std::_Any_data&amp;, _ArgTypes ...) [with _Res = opt&lt;int&gt;; _Functor = main()::&lt;lambda(int)&gt;; _ArgTypes = {int}]’
  assoc_array.cc:7:29: note:   because the following virtual functions are pure within ‘opt&lt;int&gt;’:
  assoc_array.cc:11:15: note:     int opt&lt;T&gt;::isEmpty() [with T = int]
  assoc_array.cc:12:13: note:     T opt&lt;T&gt;::getObject() [with T = int]
  In file included from assoc_array.cc:3:0:
  /usr/include/c++/4.7/functional:1912:40: error: cannot allocate an object of abstract type ‘opt&lt;int&gt;’
  assoc_array.cc:7:29: note:   since type ‘opt&lt;int&gt;’ has pure virtual functions
  In file included from assoc_array.cc:3:0:
  /usr/include/c++/4.7/functional:1743:2: error: ‘static _Functor* std::_Function_base::_Base_manager&lt;_Functor&gt;::_M_get_pointer(const std::_Any_data&amp;) [with _Functor = main()::&lt;lambda(int)&gt;]’, declared using local type ‘main()::&lt;lambda(int)&gt;’, is used but never defined [-fpermissive]
  </code></pre>
  
  <p>Where is my error using the closures with the static casting to an interface? I've already read similar replies (<a href="https://stackoverflow.com/questions/4785207/c-and-inheritance-in-abstract-classes">C++ and inheritance in abstract classes</a>, <a href="https://stackoverflow.com/questions/1615902/c-member-function-not-declared-in-derived-class">C++ - &quot;Member function not declared&quot; in derived class</a>), but it seems that no error is given at compile time if I remove the closure definition. Thanks in advance.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Even with the following code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;unordered_map&gt;
  #include &lt;functional&gt;
  #include &lt;iostream&gt;
  using namespace std;;
  
  template &lt;typename T&gt; class opt {
      public:
          opt&lt;T&gt;(T) {};
          ~opt&lt;T&gt;() {};
          virtual int isEmpty() = 0;
          virtual T getObject() = 0;
  };
  
  template &lt;typename T&gt; class oopt : public opt&lt;T&gt; {
      private:
          T ret;
      public:
          oopt&lt;T&gt;(T obj) { ret = obj; };
          ~oopt&lt;T&gt;() override;
          int isEmpty() { return 0; };
          T getObject() { return ret; };
  };
  int main(void) {
  
      function&lt;opt&lt;int&gt;(int)&gt; fu = [](int j) { return (oopt&lt;int&gt;(10)); };
  
      return 1;
  }
  </code></pre>
  
  <p>I still have the following compiling issues:</p>
  
  <pre><code>assoc_array.cc: In instantiation of ‘class oopt&lt;int&gt;’:
  assoc_array.cc:26:64:   required from here
  assoc_array.cc:20:4: error: ‘oopt&lt;T&gt;::~oopt() [with T = int]’ marked override, but does not override
  assoc_array.cc: In instantiation of ‘oopt&lt;T&gt;::oopt(T) [with T = int]’:
  assoc_array.cc:26:64:   required from here
  assoc_array.cc:19:19: error: no matching function for call to ‘opt&lt;int&gt;::opt()’
  assoc_array.cc:19:19: note: candidates are:
  assoc_array.cc:9:4: note: opt&lt;T&gt;::opt(T) [with T = int]
  assoc_array.cc:9:4: note:   candidate expects 1 argument, 0 provided
  assoc_array.cc:7:30: note: constexpr opt&lt;int&gt;::opt(const opt&lt;int&gt;&amp;)
  assoc_array.cc:7:30: note:   candidate expects 1 argument, 0 provided
  In file included from assoc_array.cc:3:0:
  /usr/include/c++/4.7/functional: In instantiation of ‘static _Res std::_Function_handler&lt;_Res(_ArgTypes ...), _Functor&gt;::_M_invoke(const std::_Any_data&amp;, _ArgTypes ...) [with _Res = opt&lt;int&gt;; _Functor = main()::&lt;lambda(int)&gt;; _ArgTypes = {int}]’:
  /usr/include/c++/4.7/functional:2298:6:   required from ‘std::function&lt;_Res(_ArgTypes ...)&gt;::function(_Functor, typename std::enable_if&lt;(! std::is_integral&lt;_Functor&gt;::value), std::function&lt;_Res(_ArgTypes ...)&gt;::_Useless&gt;::type) [with _Functor = main()::&lt;lambda(int)&gt;; _Res = opt&lt;int&gt;; _ArgTypes = {int}; typename std::enable_if&lt;(! std::is_integral&lt;_Functor&gt;::value), std::function&lt;_Res(_ArgTypes ...)&gt;::_Useless&gt;::type = std::function&lt;opt&lt;int&gt;(int)&gt;::_Useless]’
  assoc_array.cc:26:68:   required from here
  /usr/include/c++/4.7/functional:1909:7: error: invalid abstract return type for function ‘static _Res std::_Function_handler&lt;_Res(_ArgTypes ...), _Functor&gt;::_M_invoke(const std::_Any_data&amp;, _ArgTypes ...) [with _Res = opt&lt;int&gt;; _Functor = main()::&lt;lambda(int)&gt;; _ArgTypes = {int}]’
  assoc_array.cc:7:30: note:   because the following virtual functions are pure within ‘opt&lt;int&gt;’:
  assoc_array.cc:11:16: note:     int opt&lt;T&gt;::isEmpty() [with T = int]
  assoc_array.cc:12:14: note:     T opt&lt;T&gt;::getObject() [with T = int]
  In file included from assoc_array.cc:3:0:
  /usr/include/c++/4.7/functional:1912:40: error: cannot allocate an object of abstract type ‘opt&lt;int&gt;’
  assoc_array.cc:7:30: note:   since type ‘opt&lt;int&gt;’ has pure virtual functions
  In file included from assoc_array.cc:3:0:
  /usr/include/c++/4.7/functional:1743:2: error: ‘static _Functor* std::_Function_base::_Base_manager&lt;_Functor&gt;::_M_get_pointer(const std::_Any_data&amp;) [with _Functor = main()::&lt;lambda(int)&gt;]’, declared using local type ‘main()::&lt;lambda(int)&gt;’, is used but never defined [-fpermissive]
  </code></pre>