Получать много ошибок, в то время как random_shuffle () вектор объектов? - PullRequest
0 голосов
/ 05 апреля 2019

Я пытаюсь написать игру в Техасский Холдем, реализующую библиотеку STL.В настоящее время я работаю над созданием колоды, которая будет являться вектором объекта «Карты».Я застрял на использовании алгоритма random_shuffle (), чтобы перетасовать колоду.Я получаю тонны ошибок.Я пишу это на NetBeans 8.2 с установленным Cygwin. (Я должен) Вот ошибки, которые я получаю:

> cd 'C:\Users\vince\Documents\NetBeansProjects\texas_holdem'
> C:\cygwin64\bin\make.exe -f Makefile CONF=Debug "/usr/bin/make" -f
> nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf make[1]:
> Entering directory
> '/cygdrive/c/Users/vince/Documents/NetBeansProjects/texas_holdem'
> "/usr/bin/make"  -f nbproject/Makefile-Debug.mk
> dist/Debug/Cygwin-Windows/texas_holdem.exe make[2]: Entering directory
> '/cygdrive/c/Users/vince/Documents/NetBeansProjects/texas_holdem'
> mkdir -p build/Debug/Cygwin-Windows rm -f
> "build/Debug/Cygwin-Windows/DeckofCards.o.d" g++    -c -g -s
> -std=c++11 -MMD -MP -MF "build/Debug/Cygwin-Windows/DeckofCards.o.d" -o build/Debug/Cygwin-Windows/DeckofCards.o DeckofCards.cpp DeckofCards.cpp:42:1: error: 'vector' does not name a type; did you
> mean 'perror'?  vector<Cards> DeckofCards::get_Deck() const {  ^~~~~~ 
> perror In file included from
> /usr/lib/gcc/x86_64-pc-cygwin/7.4.0/include/c++/bits/char_traits.h:39:0,
>                  from /usr/lib/gcc/x86_64-pc-cygwin/7.4.0/include/c++/string:40,
>                  from /usr/lib/gcc/x86_64-pc-cygwin/7.4.0/include/c++/random:40,
>                  from DeckofCards.cpp:8: /usr/lib/gcc/x86_64-pc-cygwin/7.4.0/include/c++/bits/stl_algobase.h:
> In instantiation of 'void std::iter_swap(_ForwardIterator1,
> _ForwardIterator2) [with _ForwardIterator1 = __gnu_cxx::__normal_iterator<Cards*, std::vector<Cards> >; _ForwardIterator2 = __gnu_cxx::__normal_iterator<Cards*, std::vector<Cards> >]':
> /usr/lib/gcc/x86_64-pc-cygwin/7.4.0/include/c++/bits/stl_algo.h:4582:22:
> required from 'void std::random_shuffle(_RAIter, _RAIter) [with
> _RAIter = __gnu_cxx::__normal_iterator<Cards*, std::vector<Cards> >]' DeckofCards.cpp:46:61:   required from here
> /usr/lib/gcc/x86_64-pc-cygwin/7.4.0/include/c++/bits/stl_algobase.h:148:11:
> error: no matching function for call to 'swap(Cards&, Cards&)'
>        swap(*__a, *__b);
>        ~~~~^~~~~~~~~~~~ In file included from /usr/lib/gcc/x86_64-pc-cygwin/7.4.0/include/c++/bits/stl_pair.h:59:0,
>                  from /usr/lib/gcc/x86_64-pc-cygwin/7.4.0/include/c++/bits/stl_algobase.h:64,
>                  from /usr/lib/gcc/x86_64-pc-cygwin/7.4.0/include/c++/bits/char_traits.h:39,
>                  from /usr/lib/gcc/x86_64-pc-cygwin/7.4.0/include/c++/string:40,
>                  from /usr/lib/gcc/x86_64-pc-cygwin/7.4.0/include/c++/random:40,
>                  from DeckofCards.cpp:8: /usr/lib/gcc/x86_64-pc-cygwin/7.4.0/include/c++/bits/move.h:187:5:
> note: candidate: template<class _Tp> typename
> std::enable_if<std::__and_<std::__not_<std::__is_tuple_like<_Tp> >,
> std::is_move_constructible<_Tp>, std::is_move_assignable<_Tp>
> >::value>::type std::swap(_Tp&, _Tp&)
>      swap(_Tp& __a, _Tp& __b)
>      ^~~~ /usr/lib/gcc/x86_64-pc-cygwin/7.4.0/include/c++/bits/move.h:187:5:
> note:   template argument deduction/substitution failed:
> /usr/lib/gcc/x86_64-pc-cygwin/7.4.0/include/c++/bits/move.h: In
> substitution of 'template<class _Tp> typename
> std::enable_if<std::__and_<std::__not_<std::__is_tuple_like<_Tp> >,
> std::is_move_constructible<_Tp>, std::is_move_assignable<_Tp>
> >::value>::type std::swap(_Tp&, _Tp&) [with _Tp = Cards]': /usr/lib/gcc/x86_64-pc-cygwin/7.4.0/include/c++/bits/stl_algobase.h:148:11:
> required from 'void std::iter_swap(_ForwardIterator1,
> _ForwardIterator2) [with _ForwardIterator1 = __gnu_cxx::__normal_iterator<Cards*, std::vector<Cards> >; _ForwardIterator2 = __gnu_cxx::__normal_iterator<Cards*, std::vector<Cards> >]'
> /usr/lib/gcc/x86_64-pc-cygwin/7.4.0/include/c++/bits/stl_algo.h:4582:22:
> required from 'void std::random_shuffle(_RAIter, _RAIter) [with
> _RAIter = __gnu_cxx::__normal_iterator<Cards*, std::vector<Cards> >]' DeckofCards.cpp:46:61:   required from here
> /usr/lib/gcc/x86_64-pc-cygwin/7.4.0/include/c++/bits/move.h:187:5:
> error: no type named 'type' in 'struct std::enable_if<false, void>'
> /usr/lib/gcc/x86_64-pc-cygwin/7.4.0/include/c++/bits/stl_algobase.h:
> In instantiation of 'void std::iter_swap(_ForwardIterator1,
> _ForwardIterator2) [with _ForwardIterator1 = __gnu_cxx::__normal_iterator<Cards*, std::vector<Cards> >; _ForwardIterator2 = __gnu_cxx::__normal_iterator<Cards*, std::vector<Cards> >]':
> /usr/lib/gcc/x86_64-pc-cygwin/7.4.0/include/c++/bits/stl_algo.h:4582:22:
> required from 'void std::random_shuffle(_RAIter, _RAIter) [with
> _RAIter = __gnu_cxx::__normal_iterator<Cards*, std::vector<Cards> >]' DeckofCards.cpp:46:61:   required from here
> /usr/lib/gcc/x86_64-pc-cygwin/7.4.0/include/c++/bits/move.h:210:5:
> note: candidate: template<class _Tp, long unsigned int _Nm> typename
> std::enable_if<std::__is_swappable<_Tp>::value>::type std::swap(_Tp
> (&)[_Nm], _Tp (&)[_Nm])
>      swap(_Tp (&__a)[_Nm], _Tp (&__b)[_Nm])
>      ^~~~ /usr/lib/gcc/x86_64-pc-cygwin/7.4.0/include/c++/bits/move.h:210:5:
> note:   template argument deduction/substitution failed: In file
> included from
> /usr/lib/gcc/x86_64-pc-cygwin/7.4.0/include/c++/bits/char_traits.h:39:0,
>                  from /usr/lib/gcc/x86_64-pc-cygwin/7.4.0/include/c++/string:40,
>                  from /usr/lib/gcc/x86_64-pc-cygwin/7.4.0/include/c++/random:40,
>                  from DeckofCards.cpp:8: /usr/lib/gcc/x86_64-pc-cygwin/7.4.0/include/c++/bits/stl_algobase.h:148:11:
> note:   mismatched types '_Tp [_Nm]' and 'Cards'
>        swap(*__a, *__b);
>        ~~~~^~~~~~~~~~~~ In file included from /usr/lib/gcc/x86_64-pc-cygwin/7.4.0/include/c++/bits/stl_algobase.h:64:0,
>                  from /usr/lib/gcc/x86_64-pc-cygwin/7.4.0/include/c++/bits/char_traits.h:39,
>                  from /usr/lib/gcc/x86_64-pc-cygwin/7.4.0/include/c++/string:40,
>                  from /usr/lib/gcc/x86_64-pc-cygwin/7.4.0/include/c++/random:40,
>                  from DeckofCards.cpp:8: /usr/lib/gcc/x86_64-pc-cygwin/7.4.0/include/c++/bits/stl_pair.h:495:5:
> note: candidate: template<class _T1, class _T2> void
> std::swap(std::pair<_T1, _T2>&, std::pair<_T1, _T2>&)
>      swap(pair<_T1, _T2>& __x, pair<_T1, _T2>& __y)
>      ^~~~ /usr/lib/gcc/x86_64-pc-cygwin/7.4.0/include/c++/bits/stl_pair.h:495:5:
> note:   template argument deduction/substitution failed: In file
> included from
> /usr/lib/gcc/x86_64-pc-cygwin/7.4.0/include/c++/bits/char_traits.h:39:0,
>                  from /usr/lib/gcc/x86_64-pc-cygwin/7.4.0/include/c++/string:40,
>                  from /usr/lib/gcc/x86_64-pc-cygwin/7.4.0/include/c++/random:40,
>                  from DeckofCards.cpp:8: /usr/lib/gcc/x86_64-pc-cygwin/7.4.0/include/c++/bits/stl_algobase.h:148:11:
> note:   'Cards' is not derived from 'std::pair<_T1, _T2>'
>        swap(*__a, *__b);
>        ~~~~^~~~~~~~~~~~ make[2]: *** [nbproject/Makefile-Debug.mk:75: build/Debug/Cygwin-Windows/DeckofCards.o] Error 1 make[2]: Leaving
> directory
> '/cygdrive/c/Users/vince/Documents/NetBeansProjects/texas_holdem'
> make[1]: *** [nbproject/Makefile-Debug.mk:61: .build-conf] Error 2
> make[1]: Leaving directory
> '/cygdrive/c/Users/vince/Documents/NetBeansProjects/texas_holdem'
> make: *** [nbproject/Makefile-impl.mk:40: .build-impl] Error 2

Я довольно новичок в программировании.Может ли кто-нибудь объяснить мне, что я здесь делаю неправильно?Любая помощь будет оценена:)

Cards.h:

#ifndef CARDS_H
#define CARDS_H
#include <string>

class Cards {
    private:
        int rank;
        int suit;
        const int LOWEST_RANK = 0;
        const int HIGHEST_RANK = ACE;

    public:
        //In Texas hold'em, clubs is the lowest rank, and spades is the highest rank, hence clubs = 0 and spades = 3. 
        enum SUIT{
            CLUBS, DIAMONDS, HEARTS, SPADES
        };
        enum FACE {
            JACK = 11, QUEEN = 12, KING = 13, ACE = 14
        };
        Cards(int rank, int suit);
        int getRank() const;
        int getSuit() const;
        std::string toString() const;
        std::string rankToString(int rank) const;
        std::string suitToString(int suit) const;
};

std::ostream& operator<<(std::ostream& os, const Cards& c);

#endif /* CARDS_H */

Cards.cpp:

#include "Cards.h"
#include <assert.h>

Cards::Cards(int rank, int suit) {
    this->rank = rank;
    this->suit = suit;
    assert(suit == CLUBS || suit == DIAMONDS || suit == HEARTS || suit == SPADES);
    assert(rank >= LOWEST_RANK && rank <= ACE);
}

int Cards::getRank() const{
    return rank;
}

int Cards::getSuit() const{
    return suit;
}

std::string Cards::rankToString(int rank) const{
    switch (rank){
        case (JACK):
            return "Jack";
            break;
        case (QUEEN):
            return "Queen";
            break;
        case (KING):
            return "King";
            break;
        case (ACE):
            return "Ace";
            break;
        default:
            return std::to_string(rank);
    }
}

std::string Cards::suitToString(int suit) const{
    switch (suit) {
        case (CLUBS):
            return "Clubs";
            break;
        case (SPADES):
            return "Spades";
            break;
        case (DIAMONDS):
            return "Diamonds";
            break;
        case (HEARTS):
            return "Hearts";
    }
}

std::string Cards::toString() const{
    std::string retval;

    if (rank < 11)
        retval = rankToString(rank);
    else if (rank == JACK)
        retval = "J";
    else if (rank == QUEEN)
        retval = "Q";
    else if (rank == KING)
        retval = "K";
    else if (rank == ACE)
        retval = "A";

    switch (suit) {
        case (HEARTS):
            retval += "H";
            break;
        case (CLUBS):
            retval += "C";
            break;
        case (DIAMONDS):
            retval += "D";
            break;
        case (SPADES):
            retval += "S";
            break;
    }

    return retval;


}

std::ostream& operator<< (std::ostream& os, const Cards& c) {
    os << c.toString();

    return os;
}

DeckofCards.h:

#ifndef DECKOFCARDS_H
#define DECKOFCARDS_H
#include "Cards.h"
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>

class DeckofCards {
    private:
        std::vector<Cards> deck;
        const int CAPACITY = 52;
        int cards_left;
        std::vector<Cards>& get_Deck() const;

    public:
        DeckofCards();  
        ~DeckofCards();
        void shuffleDeck();
        Cards draw();
        int getCardsLeft();
        void burn_a_Card();

};


#endif /* DECKOFCARDS_H */

DeckofCards.cpp:

#include <random>

#include "DeckofCards.h"

DeckofCards::DeckofCards() {
    cards_left = CAPACITY;

    for (int i = 0; i <= 13; i++) {
        deck.push_back(Cards(i, Cards::CLUBS));
        deck.push_back(Cards(i, Cards::DIAMONDS));
        deck.push_back(Cards(i, Cards::HEARTS));
        deck.push_back(Cards(i, Cards::SPADES));
    }

    shuffleDeck();
}

DeckofCards::~DeckofCards() {
    while (!deck.empty()){
        deck.pop_back();
    }

    cards_left = 0;
}

void DeckofCards::burn_a_Card() {
    if (getCardsLeft() > 0) {
        deck.pop_back();
        cards_left--;
    }
    else {
        std::cout << "Burn failed. There is no card left in the deck!" << std::endl;
    }
}
vector<Cards> DeckofCards::get_Deck() const {
    return deck;
}
void DeckofCards::shuffleDeck(){
    std::random_shuffle(get_Deck().begin(), get_Deck().end());

}

Cards DeckofCards::draw(){
    //shuffleDeck();

    if (getCardsLeft() > 0) {
        return deck.back();
        cards_left--;
    }
    else {
        std::cout << "Draw failed. There is no card left in the deck!" << std::endl;
    }
}

int DeckofCards::getCardsLeft(){
    return cards_left;
}

Программа еще не завершена, но, поскольку я проверяю ее как есть, я почти уверен, что что-то не так с алгоритмом перемешивания, потому что, если я не выполняю перемешиваниеработает успешно.Как правильно использовать алгоритм shuffle или random_shuffle для моего массива?Я довольно новичок в кодировании.Любая помощь будет принята с благодарностью :)

1 Ответ

0 голосов
/ 05 апреля 2019

Это:

vector<Cards> DeckofCards::get_Deck() const

Должно быть:

std::vector<Cards> DeckofCards::get_Deck() const
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...