初始化类成员std :: function时编译错误?

时间:2015-07-31 19:04:33

标签: c++ c++11 function-pointers

我正在尝试使用std :: function成员创建一个类:

# include<functional>

class Widget {
public:
  std::function<int(double)> call_foo;

  Widget(std::function<int(double)> call_func)
    : call_foo(call_func)
  {};

};

但是,当我尝试初始化类时,我的代码失败了:

const int f(double x){
  if(x > 5.0){
    return 22;
  }
  return 17;
}

int main()
{
  std::function<int(double)> f1 = f;
  Widget p(f1);

}

完整的错误集如下:

 tmp/ccDoRHCh.o: In function `__static_initialization_and_destruction_0(int, int)':
widget.cpp:(.text+0x109): undefined reference to `std::ios_base::Init::Init()'
widget.cpp:(.text+0x118): undefined reference to `std::ios_base::Init::~Init()'
/tmp/ccDoRHCh.o:(.rodata._ZTIPFidE[_ZTIPFidE]+0x0): undefined reference to `vtable for __cxxabiv1::__pointer_type_info'
/tmp/ccDoRHCh.o:(.rodata._ZTIFidE[_ZTIFidE]+0x0): undefined reference to `vtable for __cxxabiv1::__function_type_info'
/tmp/ccDoRHCh.o:(.eh_frame+0xab): undefined reference to `__gxx_personality_v0'
collect2: error: ld returned 1 exit status

我正在编译启用C ++ 11。

1 个答案:

答案 0 :(得分:2)

你可能忘记了

#include <functional>

或者你没有在C ++ 11模式下编译。