Problem using a compare function inside of a class for sort()
<p>If you take a look at my function CardCompare inside of the class... It doesn't work! But, if I instead use the function where it is commented out in Hand.cpp, it works perfectly fine. Why is this?</p>

<p>Also, I am wondering if keeping the CardCompare function in my hand class makes less sense than keeping it in the Card class, if that is possible.</p>

<p>Hand.h</p>

<pre><code>#ifndef HAND_H
#define HAND_H

#include &lt;vector&gt;
#include "Card.h"

class Hand {
    private:
        std::vector&lt;Card&gt; hand;
        int total;
        void CalculateTotal();
        bool CardCompare (Card i, Card j) {return ( i.RankInt() &lt; j.RankInt() ); }//Does not work! :O
    public:
        Hand() {
            total = 0;
        }
        std::vector&lt;Card&gt; GetHand() const{ return hand;};
        void PrintHand();
        void AddToHand(Card c);

};



#endif
</code></pre>

<p>Hand.cpp</p>

<pre><code>#include "Hand.h"
#include &lt;iostream&gt;
#include &lt;algorithm&gt;

void Hand::CalculateTotal() {
    for (int i = 0; i &lt; hand.size(); i++) {
            std::cout &lt;&lt; hand[i].ToString() &lt;&lt; std::endl;
    }
}

void Hand::PrintHand() {
    for (int i = 0; i &lt; hand.size(); i++) {
        std::cout &lt;&lt; hand[i].ToString() &lt;&lt; std::endl;
    }
    std::cout &lt;&lt; std::endl;
}
/* If I place this right here, it works perfect.
bool CardCompare (Card i, Card j) {return ( i.RankInt() &lt; j.RankInt() ); }
*/
void Hand::AddToHand(Card c) {
    hand.push_back(c);
    std::sort(hand.begin(),hand.end(),CardCompare);
}

int main() {
    Hand h;
    h.PrintHand();
    h.AddToHand(Card (2, ( Card::Suit )2 ) );
    h.PrintHand();
    h.AddToHand(Card (3, ( Card::Suit )3 ) );
    h.PrintHand();
    h.PrintHand();
    h.AddToHand(Card (1, ( Card::Suit )2 ) );
    h.PrintHand();
    h.AddToHand(Card (13, ( Card::Suit )3 ) );
    h.PrintHand();

    std::cout&lt;&lt; std::endl &lt;&lt; std::endl;

    std::cout &lt;&lt; h.GetHand()[0].ToString();
}
</code></pre>

<p>Card.h</p>

<pre><code>#ifndef CARD_H
#define CARD_H

#include &lt;string&gt;

class Card {
public:
    enum Suit {
        SUIT_HEART,
        SUIT_DIAMOND,
        SUIT_CLUB,
        SUIT_SPADE
    };
    Card(int r = 1, Suit s = SUIT_HEART) : rank(r), suit(s)
    {}
    int GetRank() const { return rank; };
    Suit GetSuit() const { return suit; };
    std::string ToString() const;
    std::string SuitString() const;
    std::string RankString() const;
    int RankInt() const;


private:
    int rank;
    Suit suit;
    static const char * ranknames[];
    static const char * suitnames[];
    static const int     rankints[];
};
#endif
</code></pre>

<p>Card.cpp</p>

<pre><code>#include &lt;iostream&gt;
#include "Card.h"
//#include &lt;vector&gt; //gtfo

const char * Card::ranknames[] = { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" };
const char * Card::suitnames[] = { "Hearts", "Diamonds", "Clubs", "Spaces" };
const int    Card::rankints[]  = {11, 2, 3, 4, 5, 6, 7, 8, 9, 10 ,10 ,10, 10 };

std::string Card::ToString() const {
    std::string s = RankString();
    s.append(" of ");
    s.append(SuitString());
    return s;
}

std::string Card::SuitString() const {
    return suitnames[suit];
}

std::string Card::RankString() const {
    return ranknames[rank-1];
}

int Card::RankInt() const {
    return rankints[rank-1];
}
 /*
int main() {

    std::vector&lt;Card&gt; Deck;

    for (int i = 0; i &lt; 10 ; i++) {
        Deck.push_back(Card(i+1,(Card::Suit)((i+1)%4)));
        std::cout &lt;&lt; Deck[i].ToString() &lt;&lt; std::endl;
    }

    std::cout &lt;&lt; std::endl &lt;&lt; std::endl;
    std::random_shuffle( Deck.begin(), Deck.end() );

    for (int i = 0; i &lt; 10 ; i++) {
            std::cout &lt;&lt; Deck[i].ToString() &lt;&lt; std::endl;
    }
}*/
</code></pre>
