[Boost-users] Boost::bind: compiling error from sample code in book

7 views
Skip to first unread message

Boost lzw

unread,
Sep 2, 2009, 12:37:07 PM9/2/09
to boost...@lists.boost.org, Boost lzw
Hi there,

I am testing the following sample code on page 264 of  the book "Beyond the C++ Standard Library" and got an error msg: "result_type' : is not a member of '`global namespace'' from MSVC 2008 Pro compiler. Can anyone point out what is wrong with it? Thanks.
------------
#include<iostream>
#include<string>                                       
#include<map>
#include<vector>
#include<algorithm>
#include "boost/bind.hpp"

class print_size {                                                   
    typedef std::map<std::string, std::vector<int> > map_type;
public:
    typedef void result_type;                                       
    result_type operator()(std::ostream& os, const map_type::value_type& x) const {           
        os << x.second.size() << '\n';
    }
};

int main() {
    typedef std::map<std::string, std::vector<int> > map_type;       
    map_type m;
    m["Strange?"].push_back(1);                                   
    m["Strange?"].push_back(2);
    m["Wierd?"].push_back(3);
    m["Wierd?"].push_back(4);
    m["Wierd?"].push_back(5);

    std::for_each(m.begin(), m.end(),
        boost::bind(&print_size(), boost::ref(std::cout), _1));       
}
------------

Björn Karlsson

unread,
Sep 2, 2009, 1:12:11 PM9/2/09
to boost...@lists.boost.org
Hello,

> I am testing the following sample code on page 264 of the book "Beyond
> the C++ Standard Library" and got an error msg: "result_type' : is not
> a member of '`global namespace'' from MSVC 2008 Pro compiler. Can
> anyone point out what is wrong with it? Thanks.

> std::for_each(m.begin(), m.end(),
> boost::bind(&print_size(), boost::ref(std::cout), _1));

That should read:

std::for_each(m.begin(), m.end(),
boost::bind(print_size(), boost::ref(std::cout), _1));

In your code there's an ampersand before print_size() that shouldn't be there. Did it really look like that in the book?

Cheers,
Bjorn Karlsson
www.skeletonsoftware.net

_______________________________________________
Boost-users mailing list
Boost...@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users

Boost lzw

unread,
Sep 2, 2009, 2:34:09 PM9/2/09
to boost...@lists.boost.org
Hello Bjorn,

Oops, I made a typo myself. The code in the book is correct.
Thanks.

Robert

2009/9/2 Björn Karlsson <Bjorn.K...@readsoft.com>
Reply all
Reply to author
Forward
0 new messages