samedi 21 septembre 2019

No matching function for call in constructor - C++ 11

I am trying to create two classes: One for a Card, which holds two strings of rank and suit, and the other class for Hand, which holds an array of size 5 of Card objects.

#include <iostream>
#include <array>
#include <string>

using namespace std;

class Card
{

    public:
    explicit Card(string rank, string suit){
        this->rank = rank;
        this->suit = suit;
    }

    string getRank(){
        return rank;
    }

    string getSuit(){
        return suit;
    }

    protected:
    string rank;
    string suit;

}; 

class Hand
{
    public:
    explicit Hand(Card cards[5]){
        this->cards[5] = cards[5];
    }

    protected:
    Card cards[5];
    bool isFlush;
    bool isStraight;
    bool isRoyal;
    bool isPair;
    bool istwoPair;
    bool isTOAK;
    bool isFOAK;
};

When trying to compile, I get:

wip.cpp:33:35: error: no matching function for call to 'Card::Card()'
   33 |     explicit Hand(Card myCards[5]){
      |                                   ^

Why is the constructor getting an error? I understand the message of No matching function for call to Card::Card(), but I don't plan on instantiating this blankly. I will be creating five cards, then assigning five cards to a class. Something like:

int main(){
    Card card1("3", "Spade");
    Card card2("3", "Spade");
    Card card3("A", "Diamond");
    Card card4("K", "Heart");    
    Card card5("1", "Spade");

    Card hand1cards[5] {card1, card2, card3, card4, card5};

    Hand myHand(hand1cards);    

}

So I don't plan on having to do constructor overloading, so why am I getting this error? What can I do to fix my constructor to allow me to pass in a fixed-size array of Card objects to create a Hand object?

I have looked at a few other questions with similar problems, namely:

How do you use the non-default constructor for a member?

error: no matching function for call to

"error: no matching function for call to"

Yet none of them seem to deal with my problem (passing array of another class to this class).

Aucun commentaire:

Enregistrer un commentaire