Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ cannot pass objects of non-POD type

This is my code:

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <stdio.h>
#include <curl/curl.h>
using namespace std;
int main ()
{
    ifstream llfile;
    llfile.open("C:/log.txt");

    if(!llfile.is_open()){
        exit(EXIT_FAILURE);
    }

    string word;
    llfile >> word;
    llfile.close();
    string url = "http://example/auth.php?ll=" + word;

    CURL *curl;
    CURLcode res;

    curl = curl_easy_init();
    if(curl) {
        curl_easy_setopt(curl, CURLOPT_URL, url);
        res = curl_easy_perform(curl);

        /* always cleanup */
        curl_easy_cleanup(curl);
    }
    return 0;
}

This is my error when compiling:

main.cpp|29|warning: cannot pass objects of non-POD type 'struct std::string' through '...'; call will abort at runtime

like image 469
ash-breeze Avatar asked May 03 '12 23:05

ash-breeze


2 Answers

The problem you have is that variable argument functions do not work on non-POD types, including std::string. That is a limiation of the system and cannot be modified. What you can, on the other hand, is change your code to pass a POD type (in particular a pointer to a nul terminated character array):

curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
like image 182
David Rodríguez - dribeas Avatar answered Oct 19 '22 09:10

David Rodríguez - dribeas


As the warning indicates, std::string is not a POD-type, and POD-types are required when calling variadic-argument functions (i.e., functions with an ... argument).

However, char const* is appropriate here; change

curl_easy_setopt(curl, CURLOPT_URL, url);

to

curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
like image 20
ildjarn Avatar answered Oct 19 '22 11:10

ildjarn