can I move the array element?

In the function "ShuffleCards" which for now is just a placeholder, I am trying to move a whole card to the next card and overwrite it. The compiler won't allow that it seems, but will allow manipulation of card_deck members. Error at the bottom.

card_deck[itr].suit = i;
card_deck[itr].rank = j;

Want to move the whole card though.

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
#include<iostream>
#include<array>
#include <cstdlib> // for rand() and srand()
#include <ctime>

struct Card
{
	int rank = 0;
	int suit = 0;
};

void populate_deck(std::array<Card,52>&card_deck)
{
	 const int suit_size = 4;
	 const int rank_size = 13;
	 int itr(0);
	 for(int i=1; i<=(card_deck.size()/rank_size);++i)
	  {
		  for(int j=1; j<=(card_deck.size()/suit_size);++j)
	  {

		  card_deck[itr].suit = i;
		  card_deck[itr].rank = j;
		  std::cout<<card_deck[itr].suit<<"\t"<<card_deck[itr].rank<<std::endl;
		  itr+=1;
	  }
	  }
}

void read_back_array(std::array<Card,52>&card_deck)
{
	std::cout<<"Suit"<<"\t"<<"Rank"<<std::endl;
	for (auto &elem:card_deck)
		std::cout<<elem.suit<<"\t"<<elem.rank<<std::endl;
}
int getRandomNumber(int min, int max)
	{

	    static const double fraction = 1.0 / (static_cast<double>(RAND_MAX) + 1.0);  // static used for efficiency, so we only calculate this value once
	    // evenly distribute the random number across our range
	    return static_cast<int>(rand() * fraction * (max - min + 1) + min);
	}
void shuffle_deck(std::array<Card,52>card_deck)
{
	std::cout<<"Shuffle Deck"<<std::endl;
	for (int ctr = 0; ctr<card_deck.size(); ++ctr)
	{
		std::cout<<card_deck[ctr] = card_deck[++ctr]; //here trying to move card_deck objects
		++ctr;
	}

}

int main()
{
	std::array<Card,52>card_deck;
	populate_deck(card_deck);
	read_back_array(card_deck);
	srand(static_cast<unsigned int>(time(0))); // set initial seed value to system clock
	shuffle_deck(card_deck);

	return 0;
}




.\src\main.cpp:48:27: error: cannot bind 'std::ostream {aka std::basic_ostream<char>}' lvalue to 'std::basic_ostream<char>&&'
   std::cout<<card_deck[ctr] = card_deck[ctr];
Last edited on
Ok,
On line 48 you wrote:
 
std::cout<<card_deck[ctr] = card_deck[++ctr];

You are trying to assign an ostream to a variable. Instead, write:
 
 card_deck[ctr] = card_deck[++ctr];

Why were you using cout?
Anyway, it should work now
Good luck!
Thx. Moving forwards.
Function shuffle_deck() should have the deck passed by reference with the & operator, so it can modify the array.

You might consider
#include <algorithm>

1
2
3
4
void shuffle_deck(std::array<Card, 52> &card_deck)
{
    std::random_shuffle(card_deck.begin(), card_deck.end());
}

Topic archived. No new replies allowed.