Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
152 views
in Technique[技术] by (71.8m points)

c++ - curl WRITEFUNCTION and classes

class Filter{
private:
    string contents;
    bool Server(void);
public:
    void handle(void *, size_t, size_t, void *);
   };

i have a class header like this. i want to call curl WRITEFUNCTION inside the function Server which would use handle to write to the string contents. although it keeps giveng me the error

error: invalid use of member (did you forget the ‘&’ ?)

the line pointed by error is that of CURLOPT_WRITEFUNCTION.... My curl request looks something like this...

curl_easy_setopt(curl,CURLOPT_URL, address.c_str());
curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,handle);
curl_easy_perform(curl);

that means its unable to access the handle().. how can i rectify this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

handle must be a static member function. You can pass a pointer to the instance of Filter as last argument by using CURLOPT_WRITEDATA

class Filter
{ 
private:
    std::string content_;
    static size_t handle(char * data, size_t size, size_t nmemb, void * p);
    size_t handle_impl(char * data, size_t size, size_t nmemb);
};

size_t Filter::handle(char * data, size_t size, size_t nmemb, void * p)
{
    return static_cast<Filter*>(p)->handle_impl(data, size, nmemb);
}

size_t Filter::handle_impl(char* data, size_t size, size_t nmemb)
{
    content_.append(data, size * nmemb);
    return size * nmemb;
}

int main()
{
   // curl initialization... should be done in a constructor
   Filter f;
   curl_easy_setopt(curl, CURLOPT_WRITEDATA, &f);
   curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &Filter::handle);
   // curl cleanup... should be done in a destructor
   return 0;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...