Problems with tr1::bind and reference to abstract class

Discussion in 'C++' started by Neil Morgenstern, Aug 21, 2009.

  1. This is a workable example of what I am trying to do in the real
    situation that reproduces the error. I could not tryitout on Comeau
    which does not seem to recognise tr1, at least not in the standard
    headers.

    I am trying to compile it on VC2008 and am including the relevant
    headers for it.

    -------------

    #include <memory>
    #include <algorithm>
    #include <functional>
    #include <vector>
    #include <iostream>
    #include <sstream>

    class Abstract1
    {
    public:
    virtual ~Abstract1()
    {
    }

    virtual std::string gimme() const = 0;
    };

    class Derived1 : public Abstract1
    {
    std::string gimme() const
    {
    return "Take It\n";
    }
    };

    class Abstract2
    {
    public:
    virtual ~Abstract2()
    {
    }


    virtual void getme( const Abstract1 & abs1, std::eek:stream & ostr ) =
    0;
    };

    class Derived2 : public Abstract2
    {
    public:
    void getme( const Abstract1 & abs1, std::eek:stream & ostr )
    {
    ostr << abs1.gimme();
    }
    };

    using std::tr1::shared_ptr;
    using std::tr1::bind;
    using std::tr1::placeholders::_1;
    using std::tr1::ref;
    using std::tr1::cref;

    int main()
    {
    shared_ptr< Abstract1 > ptr1( new Derived1 );

    typedef shared_ptr< Abstract2 > Ptr2;

    std::vector< Ptr2 > ptr2Vec;

    ptr2Vec.push_back( Ptr2( new Derived2 ) );
    std::eek:stringstream oss;

    std::for_each( ptr2Vec.begin(), ptr2Vec.end(),
    bind( &Abstract2::getme, _1, cref( *ptr1 ), ref( oss ) ) );

    std::cout << oss.str();
    }

    ---------

    The error I am getting is that Abstract1 is abstract, no problem it
    would seem with Abstract2, but because I am passing with the cref
    modifier that should get around the problem and pass it as a const
    reference (as the function getme requires).

    I get the same error if I dereference ptr1 first and put cref around
    the const reference, and also I get the error even if I use a non-
    const reference.

    In my real code I did the simple workaround of a functor (which took a
    few seconds to write) but I'd rather be able to use bind than have to
    write functors.
    Neil Morgenstern, Aug 21, 2009
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matthias Kaeppler
    Replies:
    1
    Views:
    418
    R.F. Pels
    May 22, 2005
  2. Gert Van den Eynde

    tr1::bind in a template class

    Gert Van den Eynde, Dec 12, 2005, in forum: C++
    Replies:
    2
    Views:
    380
    Pete Becker
    Dec 26, 2005
  3. Replies:
    4
    Views:
    800
    Rolf Magnus
    May 17, 2006
  4. Emmanuel Deloget
    Replies:
    3
    Views:
    392
    Chris Thomasson
    Mar 3, 2007
  5. Christoph Heindl

    ruby macro bind conflicts tr1 on msvc9.0

    Christoph Heindl, Jan 17, 2009, in forum: Ruby
    Replies:
    0
    Views:
    119
    Christoph Heindl
    Jan 17, 2009
Loading...

Share This Page