2014-01-27 44 views
4

我想实现的std ::绑定在一个简单的例子:如何使用std ::绑定

#include <functional> 
#include <iostream> 
using namespace std; 
using namespace std::placeholders; 
int multiply(int a, int b) 
{ 
    return a * b; 
} 
int main() 
{ 
    auto f = bind(multiply, 5, _1); 
    for (int i = 0; i < 10; i++) 
    { 
     cout << "5 * " << i << " = " << f(i) << endl; 
    } 
    return 0; 
} 
然而

,当我编译这就是我得到:

test.cpp:5: error: ‘placeholders’ is not a namespace-name 
test.cpp:5: error: expected namespace-name before ‘;’ token 
test.cpp: In function ‘int main()’: 
test.cpp:14: error: ISO C++ forbids declaration of ‘f’ with no type 
test.cpp:14: error: ‘_1’ was not declared in this scope 
test.cpp:14: error: ‘bind’ was not declared in this scope 
test.cpp:17: error: ‘f’ cannot be used as a function 

尴尬,当一个给定的示例返回编译错误OO

预先感谢您的帮助

+0

你正在使用哪种编译器?std :: bind是在C++ 11中引入的。您的编译器可能不支持C++ 11,或者您没有将正确的选项传递给编译器 – bashrc

回答

4

你的代码编译罚款,只要为-std=c++11开关传递给gcc,并与Visual Studio 2013年

这里在11月CTP编译器版本是为您的代码示例命令行编译:

g++-4.8 -std=c++11 -O2 -Wall -pedantic main.cpp 

咨询How to use g++ in terminal in mac?,了解你的选择 - 调用在xcode下或直接。

+0

我使用mac工作,并且我在2013年夏天下载了C++编译器,所以我不明白为什么它不起作用... –

+0

2013年夏是不够的。您需要下载[2013年11月CTP版本](http://www.microsoft.com/en-au/download/details.aspx?id=41151),并将其选作Visual Studio项目设置中的工具集你的项目。 – mockinterface

相关问题