title: CURLOPT_WRITEFUNCTION pointer to member function
body: <p>I'm trying to include this curl function in to my class but having trouble with CURLOPT_WRITEFUNCTION. Following the compilation didn't find me a solution. Also tried some things based on stackoverflow to no available.</p>
  
  <p>Here's my attempt (replacing 'writer' in this code)</p>
  
  <p>node::writer
  &amp;node::writer
  std::bind1st(std::mem_fun(&amp;node::writer), this);</p>
  
  <p>Here's my code:</p>
  
  <pre><code>#ifndef NODE_H_
  #define NODE_H_
  
  int writer(char *data, std::size_t size, std::size_t nmemb, std::string *buffer);
  
  /*
   * function prototypes
   */
  
  class node {
   /*
    * general struct to hold html element properties
    */
   struct tag;
  
   /*
    * the url and source of the page
    */
   std::string url;
   std::string source;
  
   /*
    *  vector of structures that store tag elements
    */
   std::vector&lt;tag&gt; heading;
   std::vector&lt;tag&gt; anchor;
  
   /*
    * grab source with curl
    */
   std::string curlHttpget(const std::string &amp;url);
  
   /*
    * add tag structs to vector
    * @see std::vector&lt;tag&gt; heading
    * @see std::vector&lt;tag&gt; anchor
    */
   void add_heading(std::string, std::string);
   void add_anchor(std::string, std::string);
  
  public:
   /*
    * constructors
    */
   node(){}
   node(std::string);
  
   /*
    * deconstructors
    */
   ~node(){}
  
   /*
    * crawl page
    */
   void load(std::string seed);//crawls the page
  
   /*
    * anchor tags
    */
   void get_anchors();// scrape the anchor tags
   void display_anchors();
  
   /*
    * heading tags
    */
   void get_headings();// scrape heading tags
   void display_headings();
  };
  /*
   * for all stored html elements
   */
  struct node::tag {
   std::string text;
   std::string properties;
   tag(std::string t, std::string p) : text(t), properties(p) {}
  };
  
  /*
   * constructors
   */
  node::node(std::string seed) {
   load(seed);
   get_anchors();
   get_headings();
  }
  /*
   * araneus::subroutines
   */
  
  // crawl the page
  void node::load(std::string seed) {
   url = seed;
   source = curlHttpget(url);
  }
  
  
  //scrape html source
  std::string node::curlHttpget(const std::string &amp;url) {
   std::string buffer;
  
   CURL *curl;
   CURLcode result;
  
   curl = curl_easy_init();
  
   if (curl) {
    curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
    curl_easy_setopt(curl, CURLOPT_HEADER, 0);
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writer);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &amp;buffer);
  
    result = curl_easy_perform(curl);//http get performed
  
    curl_easy_cleanup(curl);//must cleanup
  
    //error codes: http://curl.haxx.se/libcurl/c/libcurl-errors.html
    if (result == CURLE_OK) {
     return buffer;
    }
    //curl_easy_strerror was added in libcurl 7.12.0
    std::cerr &lt;&lt; "error: " &lt;&lt; result &lt;&lt; " " &lt;&lt; curl_easy_strerror(result) &lt;&lt; std::endl;
    return "";
   }
  
   std::cerr &lt;&lt; "error: could not initalize curl" &lt;&lt; std::endl;
   return "";
  }
  
  void node::get_headings() {
   static const regex expression("&lt;[hH][1-6]\\s*(?&lt;properties&gt;.*?)\\s*&gt;(?&lt;name&gt;.*?)&lt;/\\s*[hH][1-6]\\s*&gt;");
  
   int const subMatches[] = { 1, 2 };
  
   sregex_token_iterator p(source.begin(), source.end(), expression, subMatches);
   sregex_token_iterator end;
  
   string text;
   string properties;
  
   int count = 0;
   for (;p != end; count++, ++p)
   {
    string m(p-&gt;first, p-&gt;second);
  
    if(count % 2) {
     text = m;
     add_heading(text, properties);
    }
    else {
     properties = m;
    }
   }
  }
  
  //use regex to find anchors in source
  void node::get_anchors() {
   static const regex expression("&lt;[a|A].*?[href|HREF]\\s*=[\"\"'](?&lt;url&gt;.*?)[\"\"'].*?&gt;(?&lt;name&gt;.*?)&lt;/[a|A]&gt;");
   static const regex relative("^\\/");
   static const regex firstChar("^[A-Za-z0-9\\-_\\$\\.\\+!\\*'\\(\\)#]"); // valid url characters
   static const regex protocol("^[http:\\/\\/|HTTP:\\/\\/|https:\\/\\/|HTTPS:\\/\\/|ftp:\\/\\/|FTP:\\/\\/|sftp:\\/\\/|SFTP:\\/\\/]");
  
   int const subMatches[] = { 1, 2 };
  
   sregex_token_iterator p(source.begin(), source.end(), expression, subMatches);
   sregex_token_iterator end;
  
   string text, properties;
  
   int count = 0;
   for (; p != end; count++, ++p) {
    std::string m(p-&gt;first, p-&gt;second);
  
    if(count % 2) {
     text = m;
     add_anchor(text, properties);
    }
    else {
     if(regex_search(m, relative)) { //if link is in "/somewhere" format
      properties = url + m;
     }
     else if(regex_search(m, protocol)) { //if link is absolute "http://www.somewhere.com"
      properties = m;
     }
     else if(regex_search(m, firstChar)) { //if link starts with a valid url char "somewhere.html"
      properties = url + "/" + m;
     }
     else {
      std::cout &lt;&lt; "link of unknown protocol: " &lt;&lt; m &lt;&lt; std::endl;
     }
    }
   }
  }
  
  void node::add_heading(std::string text, std::string properties) {
   heading.push_back(tag(text, properties));
  }
  
  void node::display_headings() {
   for(int i = 0; i &lt; (int)heading.size(); i++) {
    std::cout&lt;&lt; "[h]: " &lt;&lt; heading[i].text &lt;&lt; endl;
    std::cout&lt;&lt; "[h.properties]: " &lt;&lt; heading[i].properties &lt;&lt; endl;
   }
   cout &lt;&lt; "found " &lt;&lt; (int)heading.size() &lt;&lt; " &lt;h[1-6]&gt; tags" &lt;&lt; endl;
  }
  
  void node::add_anchor(std::string text, std::string properties) {
   anchor.push_back(tag(text, properties));
  }
  
  void node::display_anchors() {
   for(int i = 0; i &lt; (int)anchor.size(); i++) {
    std::cout&lt;&lt; "[a]: " &lt;&lt; anchor[i].text &lt;&lt; endl;
    std::cout&lt;&lt; "[a.properties]: " &lt;&lt; anchor[i].properties &lt;&lt; endl;
   }
   cout &lt;&lt; "found " &lt;&lt; (int)anchor.size() &lt;&lt; " &lt;a&gt; tags" &lt;&lt; endl;
  }
  
  //required by libcurl
  int writer(char *data, std::size_t size, std::size_t nmemb, std::string *buffer) {
   int result = 0;
  
   if (buffer != NULL) {
    buffer-&gt;append(data, size * nmemb);
    result = size * nmemb;
   }
   return result;
  }
  
  #endif /* NODE_H_ */
  </code></pre>
  
  <p>looking for a solution to get the function 'int writer' to be "int node::writer". the problem occurs in std::string node::curlHttpget, when I call CURLOPT_WRITEFUNCTION. </p>
  
  <p>&amp;node::writer compiles but gives a seg fault =/</p>
  
  <p>thanks</p>