C++ 不能传递非 POD 类型的对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10440966/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 14:03:38  来源:igfitidea点击:

C++ cannot pass objects of non-POD type

c++curllibcurlcodeblocks

提问by ash-breeze

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

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

回答by David Rodríguez - dribeas

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):

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

curl_easy_setopt(curl, CURLOPT_URL, url.c_str());

回答by ildjarn

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

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

However, char const*is appropriate here; change

然而,char const*这里是合适的;改变

curl_easy_setopt(curl, CURLOPT_URL, url);

to

curl_easy_setopt(curl, CURLOPT_URL, url.c_str());