Can't firgure out member function error
I keep getting the member function error and have no idea why. Perhaps someone here could be of help.
down.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
#ifndef down_h
#define down_h
#include <string>
class Down{
//function of web page retreival
size_t write_data(char *ptr, size_t size, size_t nmemb, void *userdata);
//webpage retrieval
public:
std::string getString(const char *url);
};
#endif
|
down.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
#define CURL_STATICLIB
#include <stdio.h>
#include <curl/curl.h>
#include <curl/easy.h>
#include <string>
#include <sstream>
#include "down.h"
using namespace std;
//function of web page retreival
size_t Down::write_data(char *ptr, size_t size, size_t nmemb, void *userdata) {
std::ostringstream *stream = (std::ostringstream*)userdata;
size_t count = size * nmemb;
stream->write(ptr, count);
return count;
}
//webpage retrieval
std::string Down::getString(const char *url){
CURL *curl;
std::ostringstream stream;
CURLcode res;
curl = curl_easy_init();
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &stream);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
}
return stream.str();
}
|
main.cpp
1 2 3 4 5 6 7 8 9 10 11
|
#include "down.h"
#include <iostream>
int main(void) {
Down d;
std::cout << d.getString("www.google.com");
return 0;
}
|
Last edited on
Make it a static member function.
1 2 3 4
|
class Down{
//function of web page retreival
static std::size_t write_data( char *ptr, std::size_t size, size_t nmemb, void *userdata );
// ...
|
It's works like a charm. Thank you for a speedy reply. Just out of curiosity why does making the function static work?
A static member function does not have a this pointer, and can be called without an object of its class type.
The function for CURLOPT_WRITEFUNCTION can't be a non-static member function; it is a callback from C code (which does not know anything about C++).
That makes a lot of sense. Thanks again!
Topic archived. No new replies allowed.