代码之家  ›  专栏  ›  技术社区  ›  Jack Moody A.J. Uppal

用URL在C++中从数组中插入字符串,用LIbCURL执行GET请求

  •  0
  • Jack Moody A.J. Uppal  · 技术社区  · 8 年前

    我正在尝试使用对应于每个名称的URL对五个不同的名称发出get请求,但是xcode总是在下面的行告诉我 curl_easy_setopt(curl, CURLOPT_URL, namesURL); 它 Cannot pass object of non-trivial type 'std::__1::string' (aka 'basic_string<char, char_traits<char>, allocator<char> >') through variadic function; call will abort at runtime 。

    我的字符串插值有问题导致这个问题吗?我试过用 cout << typeid(stockURL).name() << endl; 看看我使用的是什么数据类型,但是我不理解我得到的结果。

        int main(){
            string names[5] = {"Joe", "Suzy", "Larry", "Sally", "John"};
            for(int i = 0; i < 5;i++){
              string namesURL = "http://names.com/" + names[i];
              cout << url << endl;
    
    
              CURL *curl;
              CURLcode res;
    
              curl = curl_easy_init();
              if(curl) {
                  curl_easy_setopt(curl, CURLOPT_URL, namesURL);
    
                  /* Perform the request, res will get the return code */
                  res = curl_easy_perform(curl);
                  /* Check for errors */
                  if(res != CURLE_OK)
                    fprintf(stderr, "curl_easy_perform() failed: %s\n",
                        curl_easy_strerror(res));
    
                   /* always cleanup */
                   curl_easy_cleanup(curl);
              }
            }
        }
    
    1 回复  |  直到 8 年前
        1
  •  0
  •   Jack Moody A.J. Uppal    8 年前

    博捷在评论中回答了我的问题,指出通过 namesURL.c_str() 有可能得到一个普通的 const char * 而不是 std::string .

    供参考使用 c_str() ,退房 this SO post 。

    推荐文章