3

こんにちは、単純なコードのコンパイルに問題があります。カードのデッキを実装するクラスを作成しており、list::short メソッドを使用してシャッフル メソッドを作成したいと考えています。

関連コード:

デッキ.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参照にある必要があります。この問題に対する答えをグーグルで見つけることはできません。

前もって感謝します。

4

6 に答える 6

10

言ってもいいですか :)

まず、 へのポインタを保存せずCard、カードをコンテナに直接保存します。なんらかの理由でそれらへのポインターを保存する必要がある場合は、 from を使用shared_ptr<Card>Boostます。次に、シャッフル関数を実装する代わりに、を使用std::random_shuffleして渡すことができます。random-number-generator


もう一度言います:)

これはlist、何らかの理由で使用する必要がない限り、私が念頭に置いていることです。ただし、seeその理由はありません。

#include <iostream>
#include <vector>
#include <deque>
#include <algorithm>

class Card
{
// ...
};

int main()
{
    typedef std::vector<Card> Deck;
    Deck deck;

    // ... fill deck with cards.

    // There is an optional third parameter,
    // if you need to pass YOUR random-number-generator!
    // If you do, I recommend Boost implementation.
    std::random_shuffle(deck.begin(), deck.end());
}

C++あなたは気に入らないかもしれませんが、私は で直接コンテナを扱うのが好きです。また、あなたのケースでパフォーマンスの問題があることがわかった場合は、typedef を次std::vectorのように置き換えることができます。std::deque

typedef std::deque<Card> Deck;
于 2009-09-12T17:20:57.620 に答える
7

compareRandom はメンバー関数であり、 type のbool (Deck::*)(const Card*, const Card*)ように呼び出すことができないことを意味しますf(a,b)。これは、 sort がそれを呼び出す方法です。compareRandom を静的関数またはスタンドアロン関数にするか、ファンクターを使用して Deck の特定のインスタンスに適応させることができます。

于 2009-09-12T17:16:31.597 に答える
6

ところで-並べ替えを使用してシャッフルすることはできません:)並べ替えは、比較機能についていくつかの仮定を行います。

于 2009-09-12T17:22:20.707 に答える
3

他の人が言ったことは別として、使用できます(今日学んだことです、万歳!)、ランダム関数をソート基準として使用できないと付け加えるかもしれません。std::shuffle std::random_shuffle

sortつまり、if a < b(orcompareRandom(a,b)が false を返す場合、 b < a(compareRandom(b,a)が true を返す)は falseb == aを返す必要があります。これはランダム関数では保証できませんsort。この場合の の動作は未定義です。わかりませんそれさえ終われば…

于 2009-09-12T17:29:03.017 に答える
2

Logan Capaldo's answerのエラーの理由。compareRandomこれで、次の方法でファンクターに置き換えることができます。

...
private:
struct compareRandom {
  // it shouldn't give a random compare result. 
  // sort will not work (in Visual C++ 2008 it gives runtime assert)
  bool operator()(const Card *a, const Card *b) { return rg.randomBool(); }
};
...

それからそれを使用してください

void Deck::shuffle() {
    cards->sort( compareRandom() );
}
于 2009-09-12T17:22:08.997 に答える