我使用此代码:
int send(const char*s) { CURL *curl; CURLcode res; curl_global_init(CURL_GLOBAL_ALL); curl = curl_easy_init(); if(curl) { curl_easy_setopt(curl, CURLOPT_URL, "http://localhost/query.php"); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "q=" + s); res = curl_easy_perform(curl); if(res != CURLE_OK) fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res)); curl_easy_cleanup(curl); } curl_global_cleanup(); std::cout << std::endl << "Query sent" << std::endl; return 0; }
test.cpp:199:57: error: invalid operands of types âconst char [3]â and âconst char*â to binary âoperator+â curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "q=" + s); ~~~~~^~~
你必须连接 "q=" 和 s 一个人,没有接线员 + 在cpp中,它将chars数组与指向chars的指针连接起来。创建字符串 “Q=” ,添加指向的数据 S 到这个字符串并调用 c_str() 得到 const char* 指针作为的参数 卷曲,易弯曲 功能:
"q="
s
+
“Q=”
S
c_str()
const char*
#include <string> .... curl_easy_setopt(curl, CURLOPT_URL, "http://localhost/query.php"); std::string buf("q="); buf += s; curl_easy_setopt(curl, CURLOPT_POSTFIELDS, buf.c_str());