如何使用std :: bind

时间:2014-01-27 12:33:54

标签: compiler-errors bind

我试图在一个简单的例子中实现std :: bind:

#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
当给定示例返回编译错误o.O

时,

尴尬

提前感谢您的帮助

1 个答案:

答案 0 :(得分: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或直接调用。