. Advertisement .
..3..
. Advertisement .
..4..
Recently, I ran some of my cpp code, and it gave the warning text:
[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
While searching, I realized that some people added some command lines in my sample above. But I don’t think it is the best way to correct the problem – no matching function for call to c++
How would you explain this trouble? or Is there a better way?
Below is the detail of the command that I used:
#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;
}
The cause: The deck of cards or
vector<Card> deck
which are declared in your class are private, so from outside of the class can not access to them.Solution: If you want to have a getter function, let’s follow the suggestion:
When you do this, you can call your shuffle function from main as below:
However, when calling
cout
, you will have more problems. ThedealCard
function is being called wrongly because it is a member function of a class, it should be called ascardDeck.dealCard();
instead ofdealCard(cardDeck);
.Now, let’s solve your second problem – print to standard output. This following suggestion will help you print your deal card, which is an object of type
Card
:But the cout can’t print it because it’s not a standard type. Therefore, you need to overload your << operator.
You’re trying to call
DeckOfCards::shuffle
using adeckOfCards
parameterHowever, the
vector<Card>&
is required to perform the method.These are the compiler error messages. As the compiler talks to me, I’ll paraphrase it.
Error:
Paraphrased:
Error:
Paraphrased:
Error: