Bobazonski
Bobazonski

Reputation: 1565

struct as a parameter in C++

I'm a beginner so please keep it simple.

Anyway, I have a struct defined like so:

struct card      
  {
  char rank[10];
  char suit[10];
  char color;
  bool dealt;
  char location[10];
  };

and I have a function that is passed this type of struct:

  void importCard(card deck[52]);

The problem is, if I define the struct in main(), then the compiler does not know what "card" is at the time of function declaration (above main). How do I get around this without defining the struct as a global?

Upvotes: 0

Views: 184

Answers (1)

Cheers and hth. - Alf
Cheers and hth. - Alf

Reputation: 145259

It's fine to define types as "globals", so just define the struct type at the top of the file.


By the way, note that the delaration

void importCard(card deck[52]);

is almost never written that way, because the compiler just discards the 52 in there (so that having it in the source code is a bit misleading).

Instead it's written as e.g.

void importCard(card deck[]);

And to be thorough I should mention that the coding gets a lot easier by using std::vector instead of raw arrays, and then the function would be e.g.

vector<card> importCards();

Upvotes: 5

Related Questions