|
Boost Users : |
From: Stephen Torri (storri_at_[hidden])
Date: 2007-05-21 10:58:00
Thanks for the help. Roman's idea of using std::string constructor is
better than a handcrafted loop. I just got my mind into a trap thinking
I had to create a loop. I don't see the need to change the base class
to_String to a function. My intention is to leave it as it is since vc8
and gcc both compile it fine.
Stephen
On Sun, 2007-05-20 at 22:30 -0400, Christian Henning wrote:
> Alright, I solved it. Two small changes in the code did it. Hope they
> are not too bad for you.
>
> First I needed to remove the =0 statement for the to_string in your
> base class. If I don't do that I can the following error:
>
> c:\boost_1_34_0\boost\tuple\detail\tuple_basic.hpp(328) : error C2259:
> 'Base' : cannot instantiate abstract class
> [snip]
>
> So my base class looks like as follows:
>
> class Base {
> public:
>
> virtual std::string to_String ( boost::uint32_t indent )
> { return "" ;}
> };
>
>
> Second change was to remove the boost::bind lib and to use lambda's
> bind. I also needed to add an asterisk to _1.
>
> Here is the complete code that works on my machine.
>
> #include <string>
> #include <sstream>
> #include <boost/cstdint.hpp>
> #include <vector>
> #include <boost/shared_ptr.hpp>
> #include <boost/lambda/lambda.hpp>
> #include <boost/lambda/bind.hpp>
> #include <iostream>
>
> using namespace boost::lambda;
>
> class Base {
> public:
>
> virtual std::string to_String ( boost::uint32_t indent )
> { return "" ;}
> };
>
> class A : public Base {
> public:
>
> A ( boost::uint32_t value )
> : m_value ( value )
> {}
>
> virtual std::string to_String ( boost::uint32_t indent )
> {
> std::stringstream output;
>
> for ( boost::uint32_t i = 0; i < indent; i++ )
> {
> output << " ";
> }
>
> output << m_value;
>
> return output.str();
> }
>
> private:
>
> boost::uint32_t m_value;
> };
>
> class D : public Base {
> public:
>
> D ( boost::uint32_t value )
> : m_value ( value )
> {}
>
> virtual std::string to_String ( boost::uint32_t indent )
> {
> std::stringstream output;
>
> for ( boost::uint32_t i = 0; i < indent; i++ )
> {
> output << " ";
> }
>
> output << m_value;
>
> return output.str();
> }
>
> private:
>
> boost::uint32_t m_value;
> };
>
> const char* print( const std::string& s )
> {
> return s.c_str();
> }
>
> int main ( int, char** )
> {
> typedef boost::shared_ptr<A> A_ptr_t;
> typedef boost::shared_ptr<D> D_ptr_t;
>
> typedef std::vector < boost::shared_ptr<Base> > Data_t;
> Data_t m_data;
>
> A_ptr_t a ( new A ( 50 ) );
> D_ptr_t d ( new D ( 30 ) );
>
> m_data.push_back ( a );
> m_data.push_back ( d );
>
> boost::uint32_t indent_value = 4;
>
> std::for_each ( m_data.begin(),
> m_data.end(),
> std::cout << bind( &Base::to_String,
> *_1,
> indent_value ) );
> return 0;
> }
>
> Regards,
> Christian
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net