嗨,我在编译一段简单的代码时遇到了麻烦。我正在创建一个实现一副纸牌的类,我想使用 list::short 方法创建一个 shuffle 方法。
相关代码:
甲板.h
#ifndef _DECK_H
#define _DECK_H
#include <list>
#include <ostream>
#include "Card.h"
#include "RandomGenerator.h"
using namespace std;
class Deck {
private:
static const int CARD_NUMBER = Card::CARDS_PER_SUIT*Card::SUIT_NUMBER;
list<Card *> *cards;
RandomGenerator rg;
public:
Deck();
~Deck();
void shuffle();
private:
bool const compareRandom(const Card *a, const Card *b);
};
#endif /* _DECK_H */
甲板.cc:
#include "Deck.h"
/**
* Fills the deck with a set of 52 cards
*/
Deck::Deck() {
cards = new list<Card *>();
for(int i = 0; i < CARD_NUMBER; i++)
cards->push_back(
new Card(
Card::Suit(int(i/Card::CARDS_PER_SUIT)),
i%Card::CARDS_PER_SUIT)
);
}
Deck::~Deck() {
gather();
for(list<Card *>::iterator c = cards->begin(); c != cards->end(); c++)
delete *c;
delete cards;
}
bool const Deck::compareRandom(const Card *a, const Card *b) {
return rg.randomBool();
}
void Deck::shuffle() {
cards->sort(compareRandom);
}
编译器显示下一条消息(忽略行号):
Deck.cc: In member function ‘void Deck::shuffle()’:
Deck.cc:66: error: no matching function for call to ‘std::list<Card*, std::allocator<Card*> >::sort(<unresolved overloaded function type>)’
/usr/include/c++/4.3/bits/list.tcc:303: note: candidates are: void std::list<_Tp, _Alloc>::sort() [with _Tp = Card*, _Alloc = std::allocator<Card*>]
/usr/include/c++/4.3/bits/list.tcc:380: note: void std::list<_Tp, _Alloc>::sort(_StrictWeakOrdering) [with _StrictWeakOrdering = const bool (Deck::*)(const Card*, const Card*), _Tp = Card*, _Alloc = std::allocator<Card*>]
问题必须在我没有正确使用的 compareRandom 参考上,我无法在谷歌上找到这个问题的答案。
提前致谢。