簡體   English   中英

我想念什么?

[英]What include am I missing?

我正在嘗試編譯以下程序:

#include<functional>
#include<iostream>

int main(int argc, char* argv[], char* env[]) {
  std::function<int(int, int)> f = [i, &j] { return i + j; };
  std::cout << f(5, 5);
}

為什么會出現以下錯誤:

a.cc:17:3: error: \u2018function\u2019 is not a member of \u2018std\u2019

即使我將其替換為“ auto”,編譯器也會抱怨“ f”沒有命名類型。 我嘗試使用GCC 4.4.3和4.6.2。

std::function<int(int, int)> f = [i, &j] { return i + j; };

那是錯誤的語法。

您實際上要寫的是這樣的:

std::function<int(int, int)> f =[](int i, int j) { return i + j; };

或者,如果您想使用auto ,那么:

auto f =[](int i, int j) { return i + j; };

在gcc-4.6.2中使用-std=c++0x選項來編譯此代碼。

函數對象的多態包裝是C ++ 11中的新增功能 要在支持C ++ 0x(C ++ 11的草案版本)的4.7之前的GCC安裝中使用這些功能,您需要使用-std=c++0x開關進行編譯(請參見此處 )。

對於GCC v4.7,將其切換為-std=c++11 (請參閱此處 )。

暫無
暫無

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

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