簡體   English   中英

C ++類成員函數和來自C API的回調

[英]C++ class member function and callback from C API

我正在嘗試學習如何從類中的funmain()函數調用此write_data(…)函數,如下面的代碼所示。 (我知道如果我只列出這兩個函數而不將它放在一個類中,這個程序就可以工作)。

curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data)行給出了錯誤,不允許我調用write_data(...)函數。 你可以請更正我的代碼並告訴我如何實現這一目標。 任何幫助將不勝感激。 謝謝。

error C3867: 'go_website::write_data': function call missing argument list; use '&go_website::write_data' to create a pointer to member

//Microsoft Visual Studio 10 in C++
#define CURL_STATICLIB
#include <stdio.h>
#include <curl/curl.h>
#include <curl/types.h>
#include <iostream>
#include <curl/easy.h>
#include <string>
using namespace std;

extern "C" typedef size_t curl_write_callback(void *ptr, size_t size, size_t nmemb, FILE *stream);
class go_website
{
public:
static curl_write_callback write_data;

void funmain()
{
    CURL *curl;
    FILE *fp;
    CURLcode res;
    char *url = "http://www.shorturl.com/";
    char outfilename[FILENAME_MAX] = "C:\\bbb.txt";
    curl = curl_easy_init();
    if (curl) {
        fp = fopen(outfilename,"wb");
        curl_easy_setopt(curl, CURLOPT_URL, url);
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &write_data); 
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fp);
        res = curl_easy_perform(curl);
        curl_easy_cleanup(curl);
        fclose(fp);
    }
}};

extern "C" size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) 
{
size_t written;
written = fwrite(ptr, size, nmemb, stream);
return written;
}

int main()
{
   go_website a;
   a.funmain();
   return 0;
}

有可能http://curl.haxx.se/docs/faq.html#Using_C_non_static_functions_f

 // f is the pointer to your object.
 static YourClass::func(void *buffer, size_t sz, size_t n, void *f)
 {
   // Call non-static member function.
   static_cast<YourClass*>(f)->nonStaticFunction();
 }
 // This is how you pass pointer to the static function:
 curl_easy_setopt(hcurl, CURLOPT_WRITEFUNCTION, YourClass:func);
 curl_easy_setopt(hcurl, CURLOPT_WRITEDATA, this);

這兩行不起作用:

curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fp);

第二個是最容易修復的: fp是一個文件指針,而不是一個函數,你設置了錯誤的屬性,我想你想要CURLOPT_WRITEDATA

對於回調函數,您需要一個函數指針。 普通函數的名稱會自動衰減到其地址,盡管使用address-of運算符( &functionname )更清晰。

類成員函數不會自動衰減。 實際上,非靜態類成員函數與普通函數指針完全不兼容,因為沒有辦法處理this 幸運的是,您不需要非靜態成員函數,因為在回調中不使用非靜態成員。

使回調函數為 static extern "C"

 
 
 
 
  
  
  extern "C" typedef size_t curl_write_callback(void *ptr, size_t size, size_t nmemb, FILE *stream); class go_website { public: static curl_write_callback write_data; // ... }; extern "C" size_t go_website::write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) { size_t written; written = fwrite(ptr, size, nmemb, stream); return written; }
 
 
  

然后使用address-of運算符和完全限定的函數名取其地址:

 
 
 
 
  
  
  curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &go_website::write_data);
 
 
  

甚至在錯誤消息中解釋了這部分。 但它並沒有告訴你你需要 staticextern "C" ,這是變量參數列表的問題,它們不是類型安全的。

在閱讀標准(第7.5節[dcl.link] ,特別是第4段及其示例)之后,這是不允許的。 成員函數仍然具有C ++語言鏈接,因為它的名稱(不重要)和它的類型(這是交易破壞者)。

您必須使用全局函數進行回調:

curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &write_data);

然后傳遞一個指向它的指針:

 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &write_data); 

我知道這個程序是有效的,如果我只列出這兩個函數而不將它放在一個類中

如果它在類之外工作,但不在內部,那么你可能需要使用“this”指針。

        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, this->write_data);

問題是你希望WRITEFUNCTION成為你班級的成員函數。 但是,curl不知道要調用它的類的哪個實例。 您需要創建一個傳遞給WRITEFUNCTION的靜態函數,然后將此指針作為CURLOPT_WRITEDATA參數傳遞。 然后在靜態成員函數中,您可以使用user_data指針(來自WRITE_DATA的“this”)作為類的實例。

也許這個問題會有所幫助: curl WRITEFUNCTION和類

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM