遇到 'for_each' is not a member of 'std' 的问题
来源:互联网 发布:js设置单选框选中的值 编辑:程序博客网 时间:2024/05/16 10:12
今天学Boost库时候用到 标准库的 for_each , 但是编译总是出错,提示:
error: 'for_each' is not a member of 'std' 或者 for_each was not declared in this scope
这是因为缺少头文件 #include <algorithm>,还有不要忘记using namespace std; 或者用std::for_each。
在http://ubuntuforums.org/showthread.php?t=965086的4楼找到的答案。
顺便提一下for_each用法和boost::bind调用成员函数的优点:
#include <iostream>#include <boost/bind.hpp>#include <vector>#include <algorithm>class status{ std::string name_; bool ok_;public: status(const std::string& name):name_(name),ok_(true) {} void break_it() { ok_=false; } bool is_broken() const { return ok_; } void report() const { std::cout << name_ << " is " << (ok_ ? "working nominally":"terribly broken") << '\n'; }};int main(int argc, char** argv){ std::vector<status>statuses; statuses.push_back(status("status 1")); statuses.push_back(status("status 2")); statuses.push_back(status("status 3")); statuses.push_back(status("status 4")); statuses[1].break_it(); statuses[2].break_it(); std::for_each( statuses.begin(), statuses.end(), /// std::mem_fun_ref(&status::report) boost::bind(&status::report, _1) ); return 0;}
这样就逐一调用了vector型statuses中每个status类的report成员函数,也许
这和用std::mem_fun_ref没有什么区别,但是当你改用指针时候就不同了,需要用
std::mem_fum而不是mem_fum_ref:
std::vector但是如果用boost::bind就不必担心这些了,语法是不变的。statuses; statuses.push_back(new status("status 1")); statuses.push_back(new status("status 2")); statuses.push_back(new status("status 3")); statuses.push_back(new status("status 4")); statuses[1]->break_it(); statuses[2]->break_it(); std::for_each( p_statuses.begin(), p_statuses.end(), /// std::mem_fun(&status::report) boost::bind(&status::reprot, _1) );
注:代码源自《Beyond the C++ Standard Library: An Introduction to Boost》Library9.
- 遇到 'for_each' is not a member of 'std' 的问题
- linux下编译c++程序遇到 error: ‘thread’ is not a member of ‘std’的错误
- 'find_if’ is not a member of ‘std'
- 'numeric_limits' is not a member of 'std'|
- 'numeric_limits' is not a member of 'std'
- QT中的 'rand' is not a member of 'std'问题的解决
- vc++ .net std::string is not a member of std
- regex is not a member of std—regex 不是std 的成员
- Blitz++ error: 'labs' is not a member of 'std'
- C++11 'yield' is not a member of 'std::this_thread'
- Qt 解决“is not a member of ‘std’”错误
- Code blocks error: 'thread' is not a member of 'std'|
- cocos2dx ant编译'atoi' is not a member of 'std'
- error C2039: 'wstring' : is not a member of 'std'(转)
- C++:set_union is not a member of std;
- error C2039: 'bitset' : is not a member of 'std'
- error C2039: 'min' : is not a member of 'std'
- : error: ‘cout’ is not a member of ‘std’
- Linux umask权限
- mfc下鼠标拖动画面使视图移动的实现方法,类似浏览PDF时用手型鼠标移动视图的效果
- 堆排序
- 有关java环境配置的 JAVA_HOME path classpath 问题
- js正则表达式语法
- 遇到 'for_each' is not a member of 'std' 的问题
- Ubuntu无法打开22端口
- 99精美Login(signup)登陆表单设计PSD资源
- 《Windows核心编程》读书笔记——进程篇
- Python2.6及后期版本的新特性——接口和抽象类
- android 动态改变屏幕方向
- 拥有超过20万用户的亚洲最畅销.NET报表控件ActiveReports发布v7.0版本
- 我国第一个有可能获得的诺贝尔科学奖
- log4j日志等级