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

I'm running a build in Travis CI, and during the compilation, this error occurs. I tried specifying the C++ compiler and tried using g++ but that resulted in more errors.

$ 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.
4

1 Answer

curl_easy_setopt is a C function, where variadic actually means the <cstdarg>'s ... parameter. It accepts only trivial types, which std::string is not (i.e., it cannot be copied with memcpy, and instead, a non-trivial copy-constructor is involved); otherwise the behavior is undefined or only conditionally supported. In order to pass a string value, use its const char* representation obtained with c_str():

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

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like