can't access member from for() loop

When I try to access a member from array card_deck type Card_Deck I am getting a compiler error:

src\main.cpp:29:20: error: 'struct std::array<Card_Deck, 52ull>' has no member named 'rank'
{cout<<card_deck.rank;}

What is wrong/what am I misunderstanding?

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

#include<iostream>
#include<array>

using namespace std;

struct Card_Deck
{
	int rank = 0;
	int suit = 0;
	int index = 0;
};

int main()
{
	array<Card_Deck,52>card_deck;
	const int card_suit_size = 4;
	const int card_rank_size = 13;
	int index(0);
	for(int suit(0); suit <card_suit_size; ++suit)
	for(int rank(0); rank <card_rank_size; ++rank)
	{
		card_deck[index].rank = rank;
		card_deck[index].suit = suit;
		card_deck[index].index = index;
		index+=1;
	}
	cout<<index;
	for(Card_Deck elem:card_deck)
		{cout<<card_deck.rank;}//error
}
Hey @technologist. This is exatly how a question should look like! goodjob =)

1
2
for(Card_Deck elem:card_deck)
		{cout<<card_deck.rank;}//error 

The point of ranged for-loops is that the iterator becomes the thing you are loopin. Inside the loop, "elem" is card_deck. So what you have to do is

1
2
3
4
for(Card_Deck elem:card_deck)
{
    cout << elem.rank;
}
Last edited on
@technologist,

@TarikNeaj gave you a good answer to your question. I thought I'd comment a couple of other things in your code.

Line 7. What are you actually defining with this struct? The name of your struct is important. Does a deck of cards have a rank and a suit and an index? No. A card has a rank and a suit and an index. So, you should name this struct "Card". A deck of cards would be represented by an array of Card objects.

Also, since you are creating constant values for suit size and rank size, you should use those values to size the card_deck array. When you stick a number into the middle of a statement like you did in line 16, we call that a magic number. Magic numbers are bad because they are hard to maintain when your code base grows. Magic numbers should be replaced by constant variables. So, you could move line 16 to below line 18 and replace 52 with (card_suit_size * card_rank_size)
Thx Tarik - that made my day!

Ok, more work to be done. Doug4 I am wondering: don't

1
2
const int card_suit_size = 4;
const int card_rank_size = 13;


qualify as magic numbers as well?
qualify as magic numbers as well?


No, because the idea is to use the variable name after that. The "number" exists in one place then, not in multiple locations throughout the code.

Probably should make them std::size_t type or at least one of the unsigned types. A negative number there will obviously be bad :+)

Just to throw a curve ball in :+) , consider using a enum class (scoped enum) . Then you can refer to suits and cards by name.

http://en.cppreference.com/w/cpp/language/enum
Oh, got it. : )

I've done the same exercise with enums. Good suggestion.
Last edited on
Topic archived. No new replies allowed.