遇到 'for_each' is not a member of 'std' 的问题
2012-10-26 10:01
609 查看
今天学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调用成员函数的优点:
这样就逐一调用了vector型statuses中每个status类的report成员函数,也许
这和用std::mem_fun_ref没有什么区别,但是当你改用指针时候就不同了,需要用
std::mem_fum而不是mem_fum_ref:
注:代码源自《Beyond the C++ Standard Library: An Introduction to Boost》Library9.
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 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) );但是如果用boost::bind就不必担心这些了,语法是不变的。
注:代码源自《Beyond the C++ Standard Library: An Introduction to Boost》Library9.
相关文章推荐
- QT中的 'rand' is not a member of 'std'问题的解决
- C++11 error: ‘sleep_for’ is not a member of ‘std::this_thread’
- linux下编译c++程序遇到 error: ‘thread’ is not a member of ‘std’的错误
- 平日小问题 (2):[Error] 'setprecision' is not a member of 'std'
- cocos2d-x-2.2.5 Eclipse编译报错 error: 'transform' is not a member of 'std' (IOS没有问题)
- NSString NSCFString NSCFConstantString isMemberOfClass 遇到的相关的问题
- VS2005 VS2010 error C2039: back_inserter : is not a member of std
- mysql 5.7会遇到 [Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause ...的问题
- thread is not a member of std 解决方案
- 实战c++中的智能指针unique_ptr系列-- 使用std::unique_ptr代替new operator(错误:‘unique_ptr’ is not a member of ‘std’)
- Android NDK编译C++ 'stoi' and 'to_string' is not a member of 'std'
- 'find_if’ is not a member of ‘std'
- Qt 解决“is not a member of ‘std’”错误
- C++出现to_string is not a member of std 或者 to_string was not declared in this scope的解决方法
- 实战c++中的智能指针unique_ptr系列-- 使用std::unique_ptr代替new operator(错误:‘unique_ptr’ is not a member of ‘std’)
- 【环境】Caffe安装 'unique_path' is not a member of 'boost::filesystem' 问题解决
- error C2039: 'wstring' : is not a member of 'std'(转)
- ‘numeric_limits’ is not a member of ‘std’
- 20140528 repo sync后发现的bug "replace is not a member of std"
- Qt 解决“is not a member of ‘std’”错误