title: c++ lambda expressions variables in classes
body: <p>I want to save lambda expressions variables (like in the fist code block). The problem is that then I use a classes (like the second code block) the compiler return me some errors. I don"t know how to fix it.</p>
  
  <p>I hope somebody can help me and explain, why it's not working like this. Thanks.</p>
  
  <p>First Code:</p>
  
  <pre><code>// variable for function pointer
  void (*func)(int);
  // default output function
  void my_default(int x) {
      cout &lt;&lt; "x =" &lt;&lt; "\t" &lt;&lt; x &lt;&lt; endl &lt;&lt; endl;
  }
  
  
  int main() {
      cout &lt;&lt; "Test Programm\n\n";
  
      // 1. Test - default output function
      cout &lt;&lt; "my_default\n";
      func = &amp;my_default;
      func(5);
  
      // 2. Test - special output function 2
      cout &lt;&lt; "my_func2\n";
      func = [](int x) {  cout &lt;&lt; "x =" &lt;&lt; "  " &lt;&lt; x &lt;&lt; endl &lt;&lt; endl; };
      func(5);
  
      return 0;
  }
  </code></pre>
  
  <p>Second Code:</p>
  
  <pre><code>class test {
  private:
      // variable for function pointer
      void (*func)(int);
      // default output function
      void my_default(int x) {
          cout &lt;&lt; "x =" &lt;&lt; "\t" &lt;&lt; x &lt;&lt; endl &lt;&lt; endl;
      }
  
  public:
      void dummy(void) {
          // 1. Test - default output function
          cout &lt;&lt; "my_default\n";
          func = &amp;my_default;
          func(5);
  
          // 2. Test - special output function 2
          cout &lt;&lt; "my_func2\n";
          func =  [](int x)-&gt;int{ cout &lt;&lt; "x =" &lt;&lt; "  " &lt;&lt; x &lt;&lt; endl &lt;&lt; endl; };
          func(5);
      }
  };
  
  
  // entry
  int main() {
      cout &lt;&lt; "Test Programm\n\n";
  
      test a;
      a.dummy();
  
      return 0;
  }
  </code></pre>
  
  <p>Compiler:</p>
  
  <pre><code>pi@raspberrypi ~/dev/property $ gcc -std=c++0x -o test2 test2.cpp -lstdc++
  test2.cpp: In member function ‘void test::dummy()’:
  test2.cpp:491:17: error: ISO C++ forbids taking the address of an unqualified or parenthesized non-static member function to form a pointer to member function.  Say ‘&amp;test::my_default’ [-fpermissive]
  test2.cpp:491:17: error: cannot convert ‘void (test::*)(int)’ to ‘void (*)(int)’ in assignment
  test2.cpp:496:77: error: invalid user-defined conversion from ‘test::dummy()::&lt;lambda(int)&gt;’ to ‘void (*)(int)’ [-fpermissive]
  test2.cpp:496:28: note: candidate is: test::dummy()::&lt;lambda(int)&gt;::operator int (*)(int)() const &lt;near match&gt;
  test2.cpp:496:28: note:   no known conversion for implicit ‘this’ parameter from ‘int (*)(int)’ to ‘void (*)(int)’
  </code></pre>