Attempting to reference a deleted function?

May 11, 2016 at 6:38pm
I'm still somewhat new to c++ and was working on building a program to create a deck of cards and shuffle them. I'm stuck on the last two lines of my ShuffleDeck function which are giving me the error:

"function "Card::operator=(const Card &)"(declared implicitly) cannot be referenced -- it is a deleted function"

could anyone explain whats causing this?

Deck.h
1
2
3
4
5
6
7
8
9
10
11
12
  #include "Card.h"

class Deck {
private:
	Card deck[52];
public:
	Deck();
	void BuildDeck();
	void ShuffleDeck();
	void OutputDeck();

};


Deck.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
#include "Deck.h"

Deck::Deck() {
	int k = 0;
	for (int i = 0; i < 13; i++) {
		for (int j = 0; j < 4; j++) {
			deck[k].SetCardInfo(j, i);
			k++;
		}
	}
}

void Deck::OutputDeck() {
	for (int i = 0; i < 52; i++) {
		cout << deck[i].GetCardInfo() << endl;
	}
}

void Deck::ShuffleDeck() {
	for (int i = 0; i < 52; i++) {
		int num = rand() % 52;
		Card temp = deck[num];
		deck[num] = deck[i];
		deck[i] = temp;
	}
}
Last edited on May 11, 2016 at 6:40pm
May 11, 2016 at 6:45pm
Post the definition of Card.
May 11, 2016 at 6:58pm
Oh sorry here

Card.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Card {
private:
	
	string suit, face;

public:
	Card();
	void SetCardInfo(int suit1, int face1);
	string GetCardInfo();
	const string suits[4];
	const string faces[13];
	

};

Card.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "Card.h"

Card::Card() : faces { "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace" }, suits { "H", "D", "S", "C" } {

}

void Card::SetCardInfo(int suit1, int face1) {
	 suit = suits[suit1];
	 face = faces[face1];
}

string Card::GetCardInfo() {
	return suit + ", " + face;
}
May 11, 2016 at 7:06pm
A copy or move assignment operator cannot be automatically generated by the compiler for classes with data members that are arrays (which are not copy- or move-assignable,) so you must define your own.
May 11, 2016 at 7:52pm
You could make Card::suits and Card::faces static.
May 11, 2016 at 8:28pm
@cire: arrays are not the problem, it's the const.
the assignment/copy would do an element-wise assignment/copy. In this case the assignment fails because the elements were declared constant.
May 11, 2016 at 8:37pm
@cire: arrays are not the problem, it's the const.

So it is. I don't know what I was thinking. Thanks.
May 11, 2016 at 11:29pm
Thanks for the responses guys. So is it better to change the arrays so they are no longer const or overload the assignment operator to make it work with the const arrays? Or does it not matter?
May 11, 2016 at 11:41pm
You cannot overload the assignment operator to make it work with constant arrays. The arrays are constant, meaning they cannot be changed. So, I'd go with the first strategy.
Topic archived. No new replies allowed.