error: cannot pass object of non-trivial type 'std::string' and more errors

error: cannot pass object of non-trivial type 'std::string' and more errors

我是 运行 Travis CI 的一个构建,在编译过程中,出现了这个错误。我尝试指定 C++ 编译器并尝试使用 g++,但这导致了更多错误。

$ clang -I ./jsoncpp/include/ -L ./jsoncpp/build/debug/lib -std=c++14 -v test.cpp -o buildtest.exe
...
./IBMWatson.h:79:44: error: cannot pass object of non-trivial type 'std::string'
      (aka 'basic_string<char>') through variadic function; call will abort at
      runtime [-Wnon-pod-varargs]
                curl_easy_setopt(curl, CURLOPT_PASSWORD, apikey); /* Par...
                                                         ^
./IBMWatson.h:81:39: error: cannot pass object of non-trivial type 'std::string'
      (aka 'basic_string<char>') through variadic function; call will abort at
      runtime [-Wnon-pod-varargs]
                curl_easy_setopt(curl, CURLOPT_URL, url); /* Sets Regio...
                                                    ^
./IBMWatson.h:104:102: warning: result of comparison against a string literal is
      unspecified (use strncmp instead) [-Wstring-compare]
  ...+ response.length(), &root, &err) || funcName == "returnVoices" || funcN...
...
4 warnings and 4 errors generated.
The command "clang -I ./jsoncpp/include/ -L ./jsoncpp/build/debug/lib -std=c++14 -v test.cpp -o buildtest.exe" exited with 1.

curl_easy_setopt是一个C函数,其中variadic实际上是指<cstdarg>...参数。它只接受平凡的类型,std::string 不是(即,它不能用 memcpy 复制,而是涉及一个非平凡的复制构造函数);否则行为未定义或仅有条件地支持。为了传递字符串值,请使用其 const char* 表示形式,通过 c_str():

获得
curl_easy_setopt(curl, CURLOPT_PASSWORD, apikey.c_str());
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());