简体   繁体   中英

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?

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 eg

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 eg

vector<card> importCards();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM