Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ [Error] no matching function for call to

Tags:

c++

class

I can't compile my code because of some errors.

Here some of them :

In function '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;
}
like image 358
Beginner Avatar asked Nov 11 '13 17:11

Beginner


People also ask

How do you fix a undefined reference in C++?

How To Fix Undefined Reference in C++ You can fix undefined reference in C++ by investigating the linker error messages and then providing the missing definition for the given symbols. Note that not all linker errors are undefined references, and the same programmer error does not cause all undefined reference errors.

Is private Within this context CPP?

The "private within this context" error refers to the fact that the functions addShipment , reduceInventory and getPrice are not members or friends of the class Product , so they cannot use its private members.


2 Answers

You are trying to call DeckOfCards::shuffle with a deckOfCards parameter:

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

But the method takes a vector<Card>&:

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

The compiler error messages are quite clear on this. I'll paraphrase the compiler as it talks to you.

Error:

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

Paraphrased:

Hey, pal. You're trying to call a function called shuffle which apparently takes a single parameter of type reference-to-deckOfCards, but there is no such function.

Error:

[Note] candidate is:

In file included from main.cpp

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

Paraphrased:

I mean, maybe you meant this other function called shuffle, but that one takes a reference-tovector<something>.

Error:

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

Which I'd be happy to call if I knew how to convert from a deckOfCards to a vector; but I don't. So I won't.

like image 170
John Dibling Avatar answered Sep 20 '22 17:09

John Dibling


to add to John's answer:

what you want to pass to the shuffle function is a deck of cards from the class deckOfCards that you've declared in main; however, the deck of cards or vector<Card> deck that you've declared in your class is private, so not accessible from outside the class. this means you'd want a getter function, something like this:

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;
        }
};

this will in turn allow you to call your shuffle function from main like this:

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

however, you have more problems, specifically when calling cout. first, you're calling the dealCard function wrongly; as dealCard is a memeber function of a class, you should be calling it like this cardDeck.dealCard(); instead of this dealCard(cardDeck);.

now, we come to your second problem - print to standard output. you're trying to print your deal card, which is an object of type Card by using the following instruction:

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

yet, the cout doesn't know how to print it, as it's not a standard type. this means you should overload your << operator to print whatever you want it to print when calling with a Card type.

like image 41
stellarossa Avatar answered Sep 20 '22 17:09

stellarossa