21

由于一些错误,我无法编译我的代码。

这里有一些:

在函数'int main(int, char**)'中:

[Error] no matching function for call to 'deckOfCards::shuffle(deckOfCards&)'

[Note] candidate is:

In file included from main.cpp

[Note] void deckOfCards::shuffle(std::vector<Card>&)

[Note] no known conversion for argument 1 from 'deckOfCards' to 'std::vector<Card>&'

[Error] 'dealCard' was not declared in this scope

#include <iostream>

    using namespace std;


    class Card
    {
        private:
            int m_suit;
            int m_face;
        public:
            Card(int face, int suit);
            static string suits[];
            static string faces[];
            string toString(string s_face, string s_suit);
            int getFace();
            void setFace(int face);
            int getSuit();
            void setSuit(int suit);
    };

    Card::Card(int face, int suit)
    {
        m_face = face;
        m_suit = suit;
    } 

    string Card::suits[] = {"Hearts", "Diamonds", "Clubs", "Spades"};
    string Card::faces[] = {"Ace", "Deuce", "Three", "Four", "Five", 
    "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};

    int Card::getFace(){return m_face;}
    void Card::setFace(int face){m_face = face;}
    int Card::getSuit(){return m_suit;}
    void Card::setSuit(int suit){m_suit = suit;}

    string Card::toString(string s_face, string s_suit)
    {
        string card = s_face + " of " + s_suit;
        return card;
    }





    #include <iostream>     // cout
#include <algorithm>    // random_shuffle
#include <vector>       // vector
#include <ctime>        // time
#include <cstdlib> 
#include "Card.h"

using namespace std;

class deckOfCards
{
    private:
        vector<Card> deck;

    public:
        deckOfCards();
        static int count;
        static int next;
        void shuffle(vector<Card>& deck);
        Card dealCard();
        bool moreCards();
};

int deckOfCards::count = 0;
int deckOfCards::next = 0;

deckOfCards::deckOfCards()
{
    const int FACES = 12;
    const int SUITS = 4;
    int currentCard = 0;
    for (int face = 0; face < FACES; face++)
        {
            for (int suit = 0; suit < SUITS; suit++)
                {
                    Card card = Card(face,suit); //card created and initialized
                    deck.push_back(card);
                    currentCard++;
                }
        }

}

void deckOfCards::shuffle(vector<Card>& deck)
{   
    random_shuffle(deck.begin(), deck.end());
    /*vector<Card>::iterator iter;
    for (iter = deck.begin(); iter!=deck.end(); iter++)
    {
        Card currentCard = *iter;
        random_shuffle(deck.begin(), deck.end());
        Card randomCard = deck[num];
        //change values.....
    }*/
}

Card deckOfCards::dealCard()
{
    Card nextCard = deck[next];
    next++;
    return nextCard;
}

bool deckOfCards::moreCards()
{
    if (count < 52)
    {
        count++;
        return true;
    }
    else
        return false;
}


    #include <iostream>
#include "deckOfCards.h"

using namespace std;

int main(int argc, char** argv) 
{
    deckOfCards cardDeck; // create DeckOfCards object
    cardDeck.shuffle(cardDeck); // shuffle the cards in the deck
    while (cardDeck.moreCards() == true)
        {
            cout << dealCard(cardDeck);// deal the cards in the deck
        }
    return 0;
}
4

2 回答 2

68

您正在尝试DeckOfCards::shuffle使用deckOfCards参数调用:

deckOfCards cardDeck; // create DeckOfCards object
cardDeck.shuffle(cardDeck); // shuffle the cards in the deck

但该方法需要一个vector<Card>&

void deckOfCards::shuffle(vector<Card>& deck)

编译器错误消息对此非常清楚。我会在编译器与您交谈时对其进行解释。

错误:

[Error] no matching function for call to 'deckOfCards::shuffle(deckOfCards&)'

释义:

嘿,伙计。您正在尝试调用一个被调用的函数,该函数shuffle显然接受一个类型为 reference-to- 的参数deckOfCards,但没有这样的函数。

错误:

[Note] candidate is:

In file included from main.cpp

[Note] void deckOfCards::shuffle(std::vector&)

释义:

我的意思是,也许你的意思是另一个名为 的函数shuffle,但那个函数需要一个引用vector<something>

错误:

[Note] no known conversion for argument 1 from 'deckOfCards' to 'std::vector&'

如果我知道如何从 a 转换为 a ,我会很乐意打电话 deckOfCardsvector; 但我没有。所以我不会。

于 2013-11-11T17:59:09.327 回答
7

添加到约翰的答案:

您要传递给函数的是您在 main 中声明shuffle的类中的一副牌;deckOfCards但是,纸牌或vector<Card> deck您在课堂上声明的纸牌是私有的,因此无法从课堂外访问。这意味着您需要一个 getter 函数,如下所示:

class deckOfCards
{
    private:
        vector<Card> deck;

    public:
        deckOfCards();
        static int count;
        static int next;
        void shuffle(vector<Card>& deck);
        Card dealCard();
        bool moreCards();
        vector<Card>& getDeck() {   //GETTER
            return deck;
        }
};

反过来,这将允许您从 main 调用您的 shuffle 函数,如下所示:

deckOfCards cardDeck; // create DeckOfCards object
cardDeck.shuffle(cardDeck.getDeck()); // shuffle the cards in the deck

但是,您有更多问题,特别是在调用cout. 首先,您dealCard错误地调用了该函数;作为dealCard一个类的成员函数,你应该这样调用它cardDeck.dealCard();而不是 this dealCard(cardDeck);

现在,我们来解决您的第二个问题 - 打印到标准输出。您正在尝试Card使用以下说明打印您的交易卡,这是一个类型的对象:

cout << cardDeck.dealCard();// deal the cards in the deck

然而,cout不知道如何打印它,因为它不是标准类型。这意味着您应该重载<<运算符以在使用类型调用时打印您想要打印的任何内容Card

于 2013-11-11T18:09:00.227 回答