【问题标题】:C++ cannot pass objects of non-POD typeC++ 不能传递非 POD 类型的对象
【发布时间】:2012-05-13 12:48:54
【问题描述】:

这是我的代码:

#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;
}

这是我编译时的错误:

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

【问题讨论】:

  • +1 表示 SSCCE,-1 表示实际上没有提出问题。呃,+0 我猜...

标签: c++ curl libcurl codeblocks


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

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

    curl_easy_setopt(curl, CURLOPT_URL, url);
    

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

    【讨论】:

      猜你喜欢
      • 2014-10-17
      • 2018-07-20
      • 1970-01-01
      • 1970-01-01
      • 2013-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多