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

2. Marcus KwokGuest

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