[英]C++ [Error] no matching function for call to
I can't compile my code because of some errors. 由于一些错误,我无法编译我的代码。
Here some of them : 其中一些:
In function 'int main(int, char**)': 在函数'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;
}
You are trying to call DeckOfCards::shuffle
with a deckOfCards
parameter: 您正尝试使用deckOfCards
参数调用DeckOfCards::shuffle
:
deckOfCards cardDeck; // create DeckOfCards object
cardDeck.shuffle(cardDeck); // shuffle the cards in the deck
But the method takes a vector<Card>&
: 但该方法采用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. 你试图调用一个名为shuffle
的函数,它显然需要一个类型为reference-to-deckOfCards
参数,但是没有这样的函数。
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>
. 我的意思是,也许你的意思是另一个叫做shuffle
函数,但是那个引用了vector<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 avector
; 如果我知道如何从deckOfCards
转换为vector
,我会很高兴地打电话给我; but I don't. 但我没有。 So I won't. 所以我不会。
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; 你想要传递给shuffle
函数的是来自class deckOfCards
,你已经在主要声明; 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. 但是,您在课堂上宣布的卡片组或vector<Card> deck
组是私人的,因此无法从课堂外访问。 this means you'd want a getter function, something like this: 这意味着你需要一个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;
}
};
this will in turn allow you to call your shuffle function from main like this: 这将允许您从main调用您的shuffle函数,如下所示:
deckOfCards cardDeck; // create DeckOfCards object
cardDeck.shuffle(cardDeck.getDeck()); // shuffle the cards in the deck
however, you have more problems, specifically when calling cout
. 但是,你有更多的问题,特别是在调用cout
。 first, you're calling the dealCard
function wrongly; 首先,你错误地调用了dealCard
函数; as dealCard
is a memeber function of a class, you should be calling it like this cardDeck.dealCard();
因为dealCard
是一个类的dealCard
函数,你应该像这个cardDeck.dealCard();
instead of this dealCard(cardDeck);
而不是这个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: 您正在尝试使用以下说明打印您的交易卡,这是Card
类型的对象:
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. 然而, cout
不知道如何打印它,因为它不是标准类型。 this means you should overload your <<
operator to print whatever you want it to print when calling with a Card
type. 这意味着你应该重载 <<
运算符,以便在使用Card
类型调用时打印您想要打印的任何内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.