错误:无法传递非平凡类型"std::string"的对象和更多错误

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

本文关键字:错误 string std 对象 类型      更新时间:2023-10-16

我正在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函数,其中可变参数实际上表示<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());
相关文章: