سؤال

مرحبا أواجه مشكلة تجميع قطعة بسيطة من التعليمات البرمجية.أنا خلق فئة تنفذ مجموعة من البطاقات ، أريد إنشاء المراوغة طريقة استخدام القائمة::القصير الأسلوب.

كود ذات الصلة:

سطح السفينة.ح

#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 */

deck.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 الإشارة أنني لا تستخدم بشكل صحيح, أنا غير قادر على العثور على غوغلينغ الجواب على هذه المشكلة.

شكرا مقدما.

هل كانت مفيدة؟

المحلول

هل يمكن أن أقول شيئا :)

أولا, لا مؤشر إلى مخزن Card, فقط تخزين بطاقات مباشرة في الحاوية.إذا كنت تصر على تخزين مؤشرات لهم لأي سبب من الأسباب ، استخدام 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++, ، على الرغم من أنك قد لا ترغب في ذلك.أيضا, إذا كنت ترى أن std::vector لديه مشكلات في الأداء في حالة الخاصة بك فقط يمكن أن تحل محل الرموز المميزة ل typedef مع std::deque:

typedef std::deque<Card> Deck;

نصائح أخرى

compareRandom عضو وظيفة ، فقد نوع bool (Deck::*)(const Card*, const Card*) مما يعني أنك لا يمكن أن نسميها مثل f(a,b), الذي هو كيفية فرز سيتم تسميتها.يمكنك جعل compareRandom ثابت مستقل أو وظيفة ، أو استخدام functor لتكييفه مع نسخة معينة من سطح السفينة.

راجع للشغل - لا يمكنك المراوغة باستخدام نوع :) نوعا ما يجعل بعض الافتراضات على مقارنة الوظيفة.

وبصرف النظر عن الآخرين ما قال:يمكنك استخدام std::shuffle std::random_shuffle (شيء تعلمت اليوم يا هلا!), وأود أن أضيف لك لا استخدام عشوائي وظيفة كنوع المعيار.

sort يأخذ صارمة ضعف الطلب كما المقارنة, وهذا يعني أنه إذا a < b (أو compareRandom(a,b) ترجع كاذبة ، ثم b < a (compareRandom(b,a) إرجاع true) ، b == a يجب أن return false ، والتي لا يمكن أبدا ضمان مع وظيفة العشوائية.سلوك sort غير معرف في هذه الحالة.أنا لا أعرف ما إذا كان حتى ينتهي...

سبب الخطأ في لوجان Capaldo الجواب.الآن هل يمكن أن تحل محل compareRandom مع functor بالطريقة التالية:

...
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() );
}

وأحثكم على استخدام std::random_shuffle بدلا من ذلك.فهو لن يعمل على list ولكن سوف تعمل على deque أو vector, إلا إذا كنت بحاجة قائمة خصائص أقترح عليك استخدام وعاء آخر.إذا كان يجب استخدام قائمة جرب هذا:

void Deck::shuffle()
{
    vector<Card*> temp(cards->begin(), cards->end());
    random_shuffle(temp.begin(), temp.end());
    cards->assign(temp.begin(), temp.end());
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top