在将random_shuffle()作为对象向量的同时出现很多错误?

时间:2019-04-05 05:08:58

标签: c++ algorithm gcc vector netbeans

我正在尝试编写实现STL库的德州扑克游戏。目前,我正在创建一个牌组,它是对象“卡片”的向量。我一直坚持使用random_shuffle()算法来洗牌。我最终收到大量错误。我在安装了cygwin的NetBeans 8.2上编写此代码。(我必须这样做) 这是我得到的错误:

public string RoleId { get; set; }
public IdentityRole Role { get; set; }

我是编码的新手。有人能启发我这里做错了什么吗?任何帮助将不胜感激:)

Cards.h:

> 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.cpp:

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

DeckofCards.h:

#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.cpp:

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

该程序尚未完成,但是正如我对其进行的实际测试一样,我很确定shuffle算法存在问题,因为如果我不进行shuffle,它将成功运行。关于我拥有的数组,使用shuffle或random_shuffle算法的正确方法是什么?我是编码新手。任何帮助将不胜感激:)

1 个答案:

答案 0 :(得分:0)

此:

?totalLocation
?ThisFile
?totalLocation & Thisfile

应该是:

vector<Cards> DeckofCards::get_Deck() const