no instance of constructor matches the argument list

while writing a code for Blackjack game in the function which makes a standard deck i am getting this message "no instance of constructor matches the argument list" I am going to show my Card.h,Hand.h, Deck.h and Deck.cpp. any help please?

Card.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
//#ifndef CARD_H
//#define CARD_H
#include <iostream>
#include <string>
#include <algorithm>
#include <ctime>
#include <vector>
using namespace std;

class Card 
{

public:
	enum rank{Ace=1,Two,Three,Four,Five,Six,Seven,Eight,Nine,Ten,Jack,Queen,King};
	enum suit{Clubs,Diamonds,Hearts,Spades};
	friend ostream &operator<<(ostream &os, const Card &aCard);
	Card();
	Card(rank r, suit s, bool ifu);
	int getValue() const;
	void Flip();
	~Card();
	
private: 
		bool isFaceUp;
		rank Rank;
		suit Suit;
};
//#endif 


Hand.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//#ifndef HAND_H
//#define HAND_H
#include "Card.h"


class Hand
{
protected:
	vector<Card*> cards;
public:
	Hand();
	void Add(Card *acard);
	void clear();
	int getTotal() const;
	virtual ~Hand();
};
//#endif 


Deck.h

1
2
3
4
5
6
7
8
9
10
11
12
13
#include "Hand.h"
#include "GenericPlayer.h"

class Deck : public Hand
{
public:
	Deck();
	void Populate();
	void Shuffle();
	void Deal(Hand &aHand);
	void AdditionalCards(GenericPlayer &aGenericPlayer);
	virtual ~Deck();
};


Deck.cpp in the line 17 I am getting that error from the compiler.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include "Deck.h"


Deck::Deck()
{
	cards.reserve(52);
	Populate();
}

void Deck::Populate()
{
	clear();

	for (int i=Card::Clubs; i <= Card::Spades; i++)
	{
		for (int j=Card::Ace; j <= Card::King; j++)
		{
			Add(new Card(static_cast<Card::rank>(j), static_cast<Card::suit>(i)));
		}
	}
	
}

void Deck::Shuffle()
{
	random_shuffle(cards.begin(), cards.end());
}

void Deck::Deal(Hand &aHand)
{
	if (!(cards.empty()))
	{
		aHand.Add(cards.back());
		cards.pop_back();
	}

	else 
	{
		cout<<"Out of cards, unable to deal"<<endl;
	}
}

void Deck::AdditionalCards(GenericPlayer &aGenericPlayer)
{
	while (!(aGenericPlayer.isBusted()) && aGenericPlayer.isHitting())
	{
		Deal(aGenericPlayer);

		if (aGenericPlayer.isBusted())
		{
			aGenericPlayer.bust();
			cout<<aGenericPlayer<<endl;
		}
	}


}


Deck::~Deck()
{

}


Last edited on
It is exactly what it says: you are calling Card(rank, rank) constructor, but you have only Card() and Card(rank, suit, bool)
Thank you but it doesn't say too few arguments.
closed account (D80DSL3A)
Because 2 arguments is too many for one and too few for the other. MiiNiPaa is correct. Why do you not supply the required bool argument at line 17?
Thank you but it doesn't say too few arguments.

Because there is no constructor with even partially similar signature.
You are calling function with signature Card(rank, rank)
Only constructor aside from default have signature Card(rank, suit, bool)
See? The second argument is suit, not rank. And you didn,t pass mandatory bool parameter.
I can understand you, it was a mistake in my code and I changed it, but the problem is not argument as when I make a cpp file and include all the codes in it instead of having separated header files and source files, I would not be faced this problem. what is the reason?

When I supply the argument with bool the compiler says too many arguments, I am fed up.
Last edited on
I recreated all files you have posted and got following error:

\Deck.cpp||In member function 'void Deck::Populate()':|
\Deck.cpp|18|error: no matching function for call to 'Card::Card(Card::rank, Card::suit)'|
\Deck.cpp|18|note: candidates are:|
\Card.h|18|note: Card::Card(Card::rank, Card::suit, bool)|
\Card.h|18|note:   candidate expects 3 arguments, 2 provided|
\Card.h|17|note: Card::Card()|
\Card.h|17|note:   candidate expects 0 arguments, 2 provided|
\Card.h|10|note: constexpr Card::Card(const Card&)|
\Card.h|10|note:   candidate expects 1 argument, 2 provided|
\Card.h|10|note: constexpr Card::Card(Card&&)|
\Card.h|10|note:   candidate expects 1 argument, 2 provided|


If i change this line to:
Add(new Card(static_cast<Card::rank>(j), static_cast<Card::suit>(i), false));
The error goes away, leaving me with a bunch of undefined references, because i don't have Card.cpp, Hand.cpp where functions are implemented.

Possible answers to your problem: a) You have messed up with project settings in your IDE. I see you have header guards commented out, maybethere is double include in your full include tree, or you have accidentally add some header to the compile list.
b) Compiler problem.
here is my Card.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include "Card.h"

Card::Card()
{
	Rank=Ace;
	Suit=Clubs;
	isFaceUp=true;
}

Card::Card(rank r, suit s, bool ifu)
{
	Rank=r;
	Suit=s;
	isFaceUp=ifu;
}

int Card::getValue() const
{
	int value=0;
	if (isFaceUp)
	{
		value=Rank;
		if (value>10)
		{
		
			value=10;
		}
	}

	return value;
	
}

void Card::Flip()
{
	isFaceUp = !isFaceUp; 
}

Card::~Card()
{

}


and here is my Hand.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include "Hand.h"


Hand::Hand()
{
	cards.reserve(10);
}

void Hand::Add (Card *acard)
{
	cards.push_back(acard);
}

void Hand::clear()
{
	
	vector<Card*>::iterator i=cards.begin();

	for (i=cards.begin(); i!=cards.end(); i++)
	{
		delete *i;
		*i=0;
	}
}

int Hand::getTotal() const
{
	if (cards.empty())
	{
		return 0;
	}
	
		
	
		if (cards[0]->getValue()==0)
		{
			return 0;
		}
	
		int total=0;
		vector<Card*>::const_iterator i;
		for (i=cards.begin(); i!=cards.end(); i++)
		{
			total+=(*i)->getValue();
		}

		bool hasAce=false;
		for (i=cards.begin(); i!=cards.end(); i++)
		{
			if ((*i)->getValue() == Card::Ace )
			{
				hasAce=true;
			}
		}

		if (hasAce && total <= 11)
		{
			total+=10;
		}
	
		return total;
}

Hand::~Hand()
{
	clear();
}
Topic archived. No new replies allowed.