未定义模板 'std::__1::function<int (double, double, double, double)>' 的隐式实例化

Posted

技术标签:

【中文标题】未定义模板 \'std::__1::function<int (double, double, double, double)>\' 的隐式实例化【英文标题】:Implicit instantiation of undefined template 'std::__1::function<int (double, double, double, double)>'未定义模板 'std::__1::function<int (double, double, double, double)>' 的隐式实例化 【发布时间】:2017-06-15 12:30:18 【问题描述】:

在我正在进行的项目中,我尝试使用curlpp 库来发出简单的html GET 请求。当我将 cpp 文件传递​​给 g++ 时,出现以下错误:

/usr/local/include/curlpp/internal/CurlHandle.hpp:185:42: error: implicit instantiation of undefined template 'std::__1::function<int (double, double, double, double)>'
curlpp::types::ProgressFunctionFunctor mProgressFunctor;
/usr/local/include/curlpp/internal/CurlHandle.hpp:134:66: error: implicit instantiation of undefined template 'std::__1::function<int (double, double, double, double)>'
void setProgressFunctor(curlpp::types::ProgressFunctionFunctor functor)

我对 c++ 还很陌生,因此我们将不胜感激。我正在开发 macOS Sierra。我使用 Homebrew 安装 curlpp(即它位于 /usr/local/Cellar)。


这是我的代码:

  1 #include <string>
  2 #include <sstream>
  3 #include <iostream>
  4 #include <curlpp/cURLpp.hpp>
  5 #include <curlpp/Easy.hpp>
  6 #include <curlpp/Options.hpp>
  7 
  8 using namespace curlpp::options;
  9 
 10 int main(int, char **)
 11    
 12     try 
 13        
 14         curlpp::Cleanup testCleanup;
 15         curlpp::Easy miRequest;
 16         miRequest.setOpt<Url>("http://www.wikipedia.org");
 17         miRequest.perform();
 18     
 19     catch(curlpp::RuntimeError & e)
 20        
 21         std::cout << e.what() << std::endl;
 22     
 23     catch(curlpp::LogicError & e)
 24        
 25         std::cout << e.what() << std::endl;
 26     
 27     
 28     return 0;
 29 

【问题讨论】:

你的代码是什么?您可以复制粘贴您尝试过的内容吗? @petersohn 完成 我试过了,没有这个错误(Ubuntu 16.04,gcc)。 @petersohn 这样代码可以编译并运行良好吗? 不,我有链接器错误。但它可以编译。 【参考方案1】:

只需通过将-std=c++11-std=c++14 添加到编译器选项来通知编译器您想使用较新的C++ 标准的功能。 std::function 只是其中之一,在 2011 年之前不存在。

【讨论】:

谢谢,我用clang++ -std=c++11 -stdlib=libc++ -I /usr/local/Cellar 编译了,还是不行。

以上是关于未定义模板 'std::__1::function<int (double, double, double, double)>' 的隐式实例化的主要内容,如果未能解决你的问题,请参考以下文章

来自 GCC 的未定义参考错误使用带有 std::vector 和特征矩阵的模板?

Travis CI 上的 Boost 中对 `std::__cxx11::basic_string 的未定义引用

[C++][原创]std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)’未定义的引用

gtest:架构 x86_64 的未定义符号与 clang++ 和 std::vector 错误

交叉编译:链接时对 std::__atomic_futex_unsigned_base::_M_futex_notify_all(unsigned int*) 的未定义引用

C ++未定义对`utils :: checkInputs的引用