0

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?

4

1 回答 1

5

将类型定义为“全局”很好,因此只需struct在文件顶部定义类型即可。


顺便说一下,注意声明

void importCard(card deck[52]);

几乎从来没有这样写,因为编译器只是丢弃了52其中的内容(因此在源代码中包含它有点误导)。

相反,它被写成例如

void importCard(card deck[]);

为了彻底,我应该提到,通过使用而不是原始数组,编码变得容易std::vector得多,然后函数将是例如

vector<card> importCards();
于 2013-02-07T21:08:04.433 回答