我想实现的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
预先感谢您的帮助
你正在使用哪种编译器?std :: bind是在C++ 11中引入的。您的编译器可能不支持C++ 11,或者您没有将正确的选项传递给编译器 – bashrc