C++无法传递非POD类型的对象

ash*_*eze 15 c++ curl codeblocks libcurl

这是我的代码:

#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;
}
Run Code Online (Sandbox Code Playgroud)

编译时这是我的错误:

的main.cpp | 29 |警告:不能通过非POD的对象类型'struct std::string'通过'...'; call将在运行时中止

Dav*_*eas 23

您遇到的问题是变量参数函数不适用于非POD类型,包括std::string.这是系统的限制,无法修改.另一方面,您可以更改代码以传递POD类型(特别是指向nul终止字符数组的指针):

curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
Run Code Online (Sandbox Code Playgroud)


ild*_*arn 11

如警告所示,std::string不是POD类型,并且在调用variadic-argument函数(即带参数的函数)时需要POD类型....

但是,char const*这里是合适的; 更改

curl_easy_setopt(curl, CURLOPT_URL, url);
Run Code Online (Sandbox Code Playgroud)

curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
Run Code Online (Sandbox Code Playgroud)