cannot pass objects of non-trivial type string

Cause of cannot pass objects of non-trivial type string

This error cannot pass objects of non-trivial type string occurs while running the Travis CI. It is very common at the time of compilation. One can try using the C++ compiler or g++ but then too this error occurs. The error for the following is: 

 

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

 

Solution

A method of representing a string of characters as a class object is defined in C++. The name of this class is std:: string. The single-byte character can be accessed thanks to the capabilities provided by the String class, which saves characters as a series of bytes.

The class of things known as strings defines those that can be represented as a stream of characters. String representation as objects prevents array degradation. When compared to character arrays in terms of implementation speed, strings are slower. Numerous features that are defined by the string class enable a variety of operations on strings.

In the C function curl easy setopt, “variadic” refers to the cstdarg parameter. It only supports trivial types, which std::string is not (it requires a non-trivial copy-constructor and cannot be copied with memcpy); otherwise, the behavior is undefined or only conditionally supported. Use the const char* representation of a string value that was obtained using c str() in order to pass it. The code for the following is as follows: 

 

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

 

One of the above solutions can work as the solution to the above problem. This solution can work for C++ as well as for the above languages. 

 

Also Read: show-doc not working in ruby pry

 

 

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *