簡體   English   中英

初始化類成員 std::function 時編譯錯誤?

[英]Compile error when initializing class member std::function?

我正在嘗試使用 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。

有可能你忘記了

#include <functional>

或者你不是在 C++11 模式下編譯。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM