【问题标题】:error: cannot pass object of non-trivial type 'std::string' and more errors错误:无法传递非平凡类型“std::string”的对象和更多错误
【发布时间】:2020-06-06 06:19:46
【问题描述】:

我正在 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.

【问题讨论】:

  • 试试这个 apikey.c_str(), url.c_str()
  • 它可以工作,但现在它输出undefined reference
  • @PySnoo 在clang 选项中使用-lcurl

标签: c++ clang c++14 travis-ci clang++


【解决方案1】:

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

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

【讨论】:

  • 好收获。似乎其他一些编译器必须检查隐式转换为普通类型,因为 gcc 不会抱怨这种行为。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-03
  • 2014-11-19
  • 1970-01-01
  • 2014-10-17
  • 1970-01-01
相关资源
最近更新 更多