错误:无法传递非平凡类型“std::string”的对象以及更多错误

4 c++ clang clang++ travis-ci c++14

我正在 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.
Run Code Online (Sandbox Code Playgroud)

Pio*_*cki 13

curl_easy_setopt是一个 C 函数,其中variadic实际上表示<cstdarg>'s...参数。它只接受普通类型,而事实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());
Run Code Online (Sandbox Code Playgroud)