2012-05-03 30 views
12

的對象,這是我的代碼:C++不能通過非POD類型

#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''...';調用將在運行時

+3

+1爲SSCCE,-1實際上不是一個問題。呃,我猜... – ildjarn

回答

21

你的問題是,可變參數函數不上非POD類型的工作,包括std::string。這是系統的限制,不能修改。是什麼就可以了,而另一方面,是改變你的代碼通過一個POD類型(特別是指向一個NUL終止的字符數組):

curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); 
11
中止

作爲警告指示,std::string不是POD型,並調用可變參數參數的函數時POD-類型是必需的(即,具有一個...參數的函數)。

但是,char const*在這裏是合適的;改變

curl_easy_setopt(curl, CURLOPT_URL, url); 

curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); 
相關問題