Libcurl CURLOPT_WRITEFUNCTION回调函数错误

时间:2016-09-20 16:06:09

标签: c++ compiler-errors libcurl

我想出了如何将curl发送到POST和GET到网页。它工作正常,并将流完美地写入文件。现在我正在尝试将其转换为名为DownloadFile的类。最终结果是能够调用成员函数,如:

download.HTTPPOST(http, postData, filename);

我在HTTPPOST成员函数中有以下代码:

void DownloadFile::HTTPPOST(const char * http, const char *postData, std::string filePath)
{
    CURL *curl;
    CURLcode res;
    std::ofstream fout;
    fout.open(filePath, std::ofstream::out | std::ofstream::app);

    /* In windows, this will init the winsock stuff */
    curl_global_init(CURL_GLOBAL_ALL);

    /* get a curl handle */
    curl = curl_easy_init();
    if (curl) 
    {
        /* First set the URL that is about to receive our POST. This URL can
        just as well be a https:// URL if that is what should receive the
        data. */
        curl_easy_setopt(curl, CURLOPT_URL, http);

        /* Now specify the POST data */
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postData);

        /* send all data to this function  */
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);

        /* we pass our 'chunk' struct to the callback function */
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &fout);

        /* Perform the request, res will get the return code */
        res = curl_easy_perform(curl);

        /* Check for errors */
        if (res != CURLE_OK)
            fprintf(stderr, "curl_easy_perform() failed: %s\n",
                curl_easy_strerror(res));

        /* always cleanup */
        curl_easy_cleanup(curl);
    }
    curl_global_cleanup();

    DownloadFile::setStatus(res);
}

这是我对write_callback成员函数的代码:

size_t DownloadFile::write_callback(char *ptr, size_t size, size_t nmemb, void *userdata)
{
    std::ofstream *fout = (std::ofstream *)userdata;
    for (size_t x = 0; x < nmemb; x++)
    {
        *fout << ptr[x];
    }

    return size * nmemb;
}

当我尝试构建这个时,我收到一个错误:

error C3867: 'DownloadFile::write_callback': non-standard syntax; use '&' to create a pointer to member

按地址传递write_callback函数之前工作正常吗?我做了它的建议'&amp;'函数前的运算符并收到此错误:

error C2276: '&': illegal operation on bound member function expression

我无法想象这一点。为什么不将write_callback识别为内存地址?我现在的印象是它在编译时没有内存地址所以它很混乱或什么?任何帮助,将不胜感激。

感谢。

0 个答案:

没有答案