a value of type "" cannot be used to initialize an entity of type ""

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "Deck.h"

Deck::Deck()
{
	card = new Card*[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::rank>(i)));
		}
	}
	
}


I have problem in line 17 and cannot convert enumerated type to integer, any help?

Thank you.
I have problem in line 17 and cannot convert enumerated type to integer, any help?


If the subject line contains the error message, that is not your problem.
What is the prototype of the Add function, and why do you think a Card* can be initialized with anything other than a memory address?
Thank you for your reply here is my 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
#ifndef CARD_H
#define CARD_H
#include <iostream>
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 


and 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()
{

}


here is my 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:
	Card **card;
public:
	Hand();
	void Add(Card **pcard);
	void clear();
	int getTotal() const;
	virtual ~Hand();
};
#endif 


here is Hand.cpp and you can find Add prototype at line 12.

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
68
69
70
#include "Hand.h"

Hand::Hand()
{
	card=new Card*[5];
	for (int i=0; i<5; i++)
	{
		card=NULL;
	}
}

void Hand::Add (Card **pcard)
{
	card=pcard;
}

void Hand::clear()
{
	
	for (int i=0; i<5; i++)
	{
		
		delete card[i];
	}
	delete [] card;
	card=NULL;
}

int Hand::getTotal() const
{
	for (int i=0; i<5; i++)
	{
		if(card[i]==NULL)
			return 0;
	}
	
		
	
		if (card[0]->getValue()==0)
		{
			return 0;
		}
	
		int total=0;
		for (int i=0; i<5; i++)
		{
			total+=card[i]->getValue();
		}

		bool hasAce=false;
		for (int i=0; i<5; i++)
		{
			if (card[i]->getValue() == Card::Ace )
			{
				hasAce=true;
			}
		}

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

Hand::~Hand()
{
	
}


I am not sure about your answer about Card* and I am looking forward for your help. do you think that it could be better to use vector instead of array?
Last edited on
do you think that it could be better to use vector instead of array?


Pretty much always when you're talking about dynamic memory.

There is no reason for Hand::card to be of type Card**. Can you think of a reason you would need an array of pointers to cards? With Add you replace the entire array of pointers with another array instead of adding a card to the hand.

With Add(new Card*(static_cast<Card::rank>(j), static_cast<Card::rank>(i))); you're trying to assign a value of type Card::rank to a Card*. That doesn't make sense.
Thank you very kindly, your answer is really helpful.
Topic archived. No new replies allowed.