cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: How come the function works but the class doesn't?

From: Julien Chaffraix <julien.chaffraix_at_gmail.com>
Date: Mon, 25 Oct 2010 21:11:40 -0700

Hi Alexander,

> Or you could use some existing C++ wrapper library for libcurl.

I advice you to follow this advice. FYI there is 3 issues in your code:

1.
size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream)
...
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);

|write_data| should be static as cURL only manipulate C-style functions.

2.
string data;
...
curl_easy_setopt(curl, CURLOPT_URL, data);

CURLOPT_URL should be a const char* not anything else. Here you should
pass data.c_str() not directly data.

3.
class rawdata {
...
string data;
...
};

rawdata::rawdata(string url)
{
    string data = url;
}

Your local variable |data| shadows the class one which prevent you
from ever setting the member |data|. It should be:

rawdata::rawdata(string url)
    : data(url)
{
}

After fixing the previous errors, the code works as expected.

Regards,
Julien
-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette: http://curl.haxx.se/mail/etiquette.html
Received on 2010-10-26