std::sort std::vector predicate problem

Discussion in 'C++' started by mscava@gmail.com, Apr 6, 2007.

  1. Guest

    How can I make this construction valid? It still gives me error about
    no matching function std::sort(...). I made a little search and wrong
    thing is probably the predicate...

    template <typename T> T Polygon<T> ::
    Height() const
    {
    class BinPred : public std::binary_function< Vector2D<T>,
    Vector2D<T>, bool >
    {
    bool operator() ( Vector2D<T> rhs, Vector2D<T> lhs )
    {
    return rhs.Y() < lhs.Y;
    }
    };

    // vertices_ is structure already filled with Vector2D's
    std::vector< Vector2D<T> > tmp = vertices_;
    std::sort( tmp.begin(), tmp.end(), BinPred() );

    return std::abs( tmp.front().Y() - tmp.back().Y() );
    }
     
    , Apr 6, 2007
    #1
    1. Advertising

  2. Marcus Kwok Guest

    wrote:
    > How can I make this construction valid? It still gives me error about
    > no matching function std::sort(...). I made a little search and wrong
    > thing is probably the predicate...
    >
    > template <typename T> T Polygon<T> ::
    > Height() const
    > {
    > class BinPred : public std::binary_function< Vector2D<T>,
    > Vector2D<T>, bool >
    > {
    > bool operator() ( Vector2D<T> rhs, Vector2D<T> lhs )
    > {
    > return rhs.Y() < lhs.Y;
    > }
    > };


    IIUC local classes do not have external linkage and therefore cannot be
    used as template arguments.

    > // vertices_ is structure already filled with Vector2D's
    > std::vector< Vector2D<T> > tmp = vertices_;
    > std::sort( tmp.begin(), tmp.end(), BinPred() );


    See above. Also, make sure you #include <algorithm>.

    > return std::abs( tmp.front().Y() - tmp.back().Y() );
    > }


    --
    Marcus Kwok
    Replace 'invalid' with 'net' to reply
     
    Marcus Kwok, Apr 6, 2007
    #2
    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. hall
    Replies:
    4
    Views:
    697
  2. Replies:
    8
    Views:
    2,005
    Csaba
    Feb 18, 2006
  3. Replies:
    4
    Views:
    491
  4. Ganesh
    Replies:
    3
    Views:
    379
    James Kanze
    Sep 30, 2008
  5. Victor Bazarov
    Replies:
    3
    Views:
    854
    James Kanze
    Feb 2, 2010
Loading...

Share This Page