libcurl in class, possible?

Hello,
I'm getting a compile error with the code below. I'm trying to get a url using the libcurl library. It works outside of the class but not within the class. Is it because its a C library? Yes. I'm a noob! :-)

What am I doing wrong?

Thank You. :-)


The error reads.
Undefined symbols for architecture x86_64:
"WebConnect::webData", referenced from:


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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <curl/curl.h>
#include <string>

using namespace std;


class WebConnect{
public:
    
    static string webData;
    
    static size_t curl_write(char *wd, size_t size, size_t nmemb, string *stream)
    {
        size_t acuallSize = size*nmemb;
        
        
        //I belieave its failing here. If I comment out this line below it compiles.
        webData.append(wd, acuallSize);
        
        return 0;
    }
    
    void getURL(string url) {
        
        CURL *curl = curl_easy_init();
        curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_write);
        curl_easy_perform(curl);
        curl_easy_cleanup(curl);

    }
    
};






int main() {
    
    
    WebConnect connection;
    connection.getURL("http://google.com/");
    
    
    
}
You need to define static variables outside the class.

 
string WebConnect::webData;

Don't remove the declaration on line 11. It is still needed.
Last edited on
Awesome thank you Peter!


below is the working code.
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
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <curl/curl.h>
#include <string>

using namespace std;

class WebConnect{
public:
    
    static string webData;
    
    static size_t curl_write(char *wd, size_t size, size_t nmemb, string *stream)
    {
        size_t acuallSize = size*nmemb;
        webData.append(wd, acuallSize);
        return 0;
    }
    
    string getURL(string url) {
        
        CURL *curl = curl_easy_init();
        curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_write);
        curl_easy_perform(curl);
        curl_easy_cleanup(curl);
        
        return webData;
    }
    
};


string WebConnect::webData;



int main() {

    WebConnect connection;
    string google = connection.getURL("http://google.com/");
    

    cout << google;
    
    
}
Topic archived. No new replies allowed.