如何修复"error: ‘_1’ was not declared in this scope"?
How to fix "error: ‘_1’ was not declared in this scope"?
我目前正在尝试将函数绑定到我打算使用的算法。
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main(){
vector<int> coll{1, 2, 3, 4, 5, 6};
vector<int>::iterator pos;
pos = find_if (coll.begin(), coll.end(),
bind(greater<int>(),_1,3));
return 0;
}
并获取此错误反馈:
AlgoTesting.cpp: In function ‘int main()’:
AlgoTesting.cpp:184:41: error: ‘_1’ was not declared in this scope
bind(greater<int>(),_1,3)); // criterion
^~
AlgoTesting.cpp:184:41: note: suggested alternative:
In file included from algostuff.hpp:15:0,
from AlgoTesting.cpp:5:
/usr/include/c++/7/functional:275:34: note: ‘std::placeholders::_1’
extern const _Placeholder<1> _1;
^~
我正在努力理解错误日志的含义。有谁知道我在这里错过了什么?
您需要包含<functional>
并使用std::placeholders::_1
pos = find_if (coll.begin(), coll.end(), bind(greater<int>(),placeholders::_1,3));
更简单的选择是使用 lambda:
pos = find_if(coll.begin(), coll.end(), [](int v) { return std::greater<int>{}(v, 3); });
或
pos = find_if(coll.begin(), coll.end(), [](int v) { return 3 < v; });
要使用std::bind
,需要包含标头#include <functional>
,并且不要忘记using namespace placeholders
。
这篇文章还提供了一些额外的信息。
相关文章:
- 如何修复"error: ‘_1’ was not declared in this scope"?
- 类中的数组变量C++导致"was not declared in this scope"实现文件的一个函数中错误,但在构造函数中不会导致错误
- 如何解决"cout was not declared in this scope"错误?
- 为什么程序显示我"was not declared in this scope"?
- 为什么我会收到"getuid was not declared in that scope"错误?
- 即使包含 <time.h 后仍"‘CLOCK_PER_SEC’ was not declared in this scope"错误>
- 如何在链接器设置和搜索目录更改后修复" '::iswblank' has not been declared"
- 我正在接受"Error: string was not declared in this scope"
- C++ 我一直在"was not declared in this scope error"
- 明确声明时显示"DialogProc not declared"错误
- C++自定义分配器:const_pointer 'has not been declared'
- 将 .ui 文件导入 Qt5 C++时出错"was not declared in this scope"
- 编译 CPP 应用程序时出错。 "error: 'posix_memalign' was not declared in this scope"
- main.cpp中的错误:函数库中的函数"not declared in this scope",但实际上是
- "Error: 'dungeon_t' was not declared in this scope"但包含在标题中
- C++ 中的" Variable ' i ' was not declared in scope "是什么?
- "pointer was not declared in this scope"是什么意思
- 学习如何在遇到错误'variable not declared in scope'的 c++ 中使用指针
- "error: not declared in this scope?"是什么意思
- 宣布时的另一个'not declared in this scope'问题