2010-11-26 71 views
4

如果你看看我的函數CardCompare裏面的類...它不工作!但是,如果我使用Hand.cpp中註釋掉的函數,它就可以很好地工作。爲什麼是這樣?使用類內部的比較函數進行排序的問題()

此外,我想知道是否保持CardCompare函數在我的手類中比保持它在Card類中沒有意義,如果可能的話。

Hand.h

#ifndef HAND_H 
#define HAND_H 

#include <vector> 
#include "Card.h" 

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

}; 



#endif 

Hand.cpp

#include "Hand.h" 
#include <iostream> 
#include <algorithm> 

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

void Hand::PrintHand() { 
    for (int i = 0; i < hand.size(); i++) { 
     std::cout << hand[i].ToString() << std::endl; 
    } 
    std::cout << std::endl; 
} 
/* If I place this right here, it works perfect. 
bool CardCompare (Card i, Card j) {return (i.RankInt() < 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<< std::endl << std::endl; 

    std::cout << h.GetHand()[0].ToString(); 
} 

Card.h

#ifndef CARD_H 
#define CARD_H 

#include <string> 

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 

Card.cpp

#include <iostream> 
#include "Card.h" 
//#include <vector> //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<Card> Deck; 

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

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

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

你可以在lea刪除所有不相關的代碼? – ybungalobill 2010-11-26 20:51:32

+0

對不起,我從現在開始做。 – Colton 2010-11-26 21:09:21

+0

個人喜好:不要將數字轉換爲`Card :: Suit`s,你應該擁有`Card :: Suit`的靜態成員,稱爲`Spades`,`Hearts`,`Diamonds`和`Clubs`。 – 2010-11-26 21:13:47

回答

7

您試圖將指針傳遞給成員函數,因此排序無法使用它,因爲它沒有this指針。你的情況,你可以只改變函數爲static

static bool CardCompare (Card i, Card j) {return (i.RankInt() < j.RankInt()); } 

如果您還需要在未來的非靜態成員函數,將其綁定與boost::bindstd::bind(對於C++ 0x中編譯) :

std::sort(hand.begin(),hand.end(),bind(&Hand::CardCompare, this, _1, _2)); 
2

如果要在sort()中使用CardCompare(),則CardCompare()不能是成員函數。您可以將卡類中的運營商<超載以比較卡。

在卡類,類似:

bool operator<(const Card& other) const { 
    return RankInt() < other.RankInt(); 
} 
1

的典型的解決方案是重載operator()代替。這會將您的課程變成仿函數,然後使用標準庫算法開箱即用。

只要改變

bool CardCompare (Card i, Card j) {return (i.RankInt() < j.RankInt()); } 

bool operator()(Card i, Card j) {return (i.RankInt() < j.RankInt()); } 

,然後你可以調用排序是這樣的:

std::sort(hand.begin(),hand.end(), Hand()); 

通常情況下,你就會把比較運營商在一個單獨的類雖然

相關問題