Confused about specialization of greater predicate??

__fletcher__
__fletcher__ used Ask the Experts™
on
I have 2 classes: Card, and CardPile.  My CardPile class has as a state a vector<const Card*>, and a method sortPile().  In the header for my CardPile class, I do the following:
template<>
struct greater<const Card*>
{
   bool operator()(const Card* leftCard, const Card* rightCard) const {
       return ( (*leftCard) < (*rightCard) );
   }
};   //(as suggested by ambience...thank you! )

I then include that file in CardPile.cc file, where there is a sortPile method that does the following:

void CardPile::sortPile() {
   sort(_cards.begin(), _cards.end(), greater<Card*>());
}  //_cards is my state which is a vector<const Card*>

All this compiles, until I try to compile it with a main function that includes CardPile's header.  It then crashes and gives me the following error:

'greater' is not a template
explicit specialization of nontemplate '{anonymous struct}'
anonymous class type not used to declare any objects

What am I doing wrong??
Thanks again to ambience for the suggestion, and I imagine it will work
when I figure out what I'm doing wrong!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
Maybe you need to include in the same header file the <functional> header?

Besides, I would have done it differently; I dont see the need to use greater<>() in sort<>(), since as a default it uses less<>(), which calls operator<() for Card.

Did you try defining the following:
bool operator<( const Card& left, const Card& right )
{ /.../ };

 enjoy.

Author

Commented:
The reason I need to use that is because I need to sort it in ascending order and I need to de-reference the Card* so that it will use my Card::operator >, rather than comparing pointers.  

I wasn't including the <functional> header, so I just tried to, and it gave me a big list of compile errors that I can't even read.  They're all from the <algorithm.h> file, and I'm not sure I could tell you what they mean.  

...any idea's??  I'm sure including <functional> is needed, but that doesn't seem to solve the problem...wait...I have an idea...

...IT WORKED!!....thanks for your help!  The problem(in case you were wondering) is that I can't put greater<const Card*>, the const was screwing things up. So I took it out.
Thanks again!!

Commented:
Dont give up on const...

define sortPile as const, and it will try to use begin() and end() in their const form.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial