简体   繁体   English

将数组传递给函数C ++

[英]Passing an array to a function c++

I'm trying to pass my array into a function called printDeck, and it was working perfectly fine until I added things to my print function. 我试图将我的数组传递给一个名为printDeck的函数,并且在向我的打印函数中添加内容之前,它运行得很好。 Here is my declaration and call to printDeck, along with calls to other functions that all work just fine: 这是我对printDeck的声明和调用,以及对其他所有正常工作的函数的调用:

void fillDeck(Card *deck);
void printDeck(Card deck[]);
void printDeck(Card p1[]);
void printDeck(Card p2[]);
void shuffleDeck(Card *deck);

int main (int argc, char *argv[]){
   Card deck[52];
   Card p1[26];
   Card p2[26];
   fillDeck(deck);
   shuffleDeck(deck);
   printDeck(deck); //this is where the problem is happening
   printDeck(p1);  //and here
   printDeck(p2);  //and here

}

the error I get is "undefined reference to `printDeck(Card*)'" for all three of those printDeck function calls. 对于所有这三个printDeck函数调用,我得到的错误是“对printDeck(Card *)的未定义引用”。 I feel like I am just making a stupid mistake and really cant see it, but everything looks just fine to me? 我觉得我只是在犯一个愚蠢的错误,真的看不到它,但是一切对我来说都很好? I've looked up syntax for passing arrays to functions and I thought I was doing it properly but perhaps not. 我查找了将数组传递给函数的语法,但我认为我做得很好,但也许做得不好。 If needed, here is the actual function: 如果需要,这是实际功能:

void printDeck(Card deck[], Card p1[], Card p2[]){
   for(int i = 0; i < 52; i++){
      printf("%d of %s",deck[i].number,deck[i].suit); 
      printf("\n\n"); 
      //printf("%s", deck[i].suit);
      //printf("\n%d\n\n", deck[i].number);
   }
   printf("\n\nP1's cards\n");
   for(int i = 0; i < 52; i++){
      printf("%d of %s", p1[i].number, p1[i].suit);
   }
   printf("\n\nP2's cards\n");
   for(int i = 0; i < 52; i++){
      printf("%d of %s", p2[i].number, p2[i].suit);
   }
}

Any help is appreciated thanks! 任何帮助表示赞赏,谢谢!

These three lines: 这三行:

void printDeck(Card deck[]);
void printDeck(Card p1[]);
void printDeck(Card p2[]);

declare the same function. 声明相同的功能。 They are equivalent to saying: 他们相当于说:

void printDeck(Card []);
void printDeck(Card []);
void printDeck(Card []);

If you want to print all the decks in one function call, you need to change the function declaration to: 如果要在一个函数调用中打印所有卡片组,则需要将函数声明更改为:

void printDeck(Card [], Card [], Card []);

and the change the calling lines from: 并将呼叫线路从:

printDeck(deck);
printDeck(p1);
printDeck(p2);

to

printDeck(deck, p1, p2);

You've defined your print deck function as: 您已将打印卡座功能定义为:

void printDeck(Card deck[]);
void printDeck(Card p1[]);
void printDeck(Card p2[]);

and yet you implemented it as: 但是您将其实现为:

void printDeck(Card deck[], Card p1[], Card p2[]){...}

You really meant to define it as: 您确实打算将其定义为:

void printDeck(Card deck[], Card p1[], Card p2[]);

and call it as 并称其为

printDeck(deck, p1, p2);

Otherwise the declaration does not match the definition. 否则,声明与定义不匹配。 Besides, three declarations of the same function will cause the compiler to attempt to generate overloads, but it will fail because all three functions have the same signature. 此外,同一函数的三个声明将导致编译器尝试生成重载,但由于所有三个函数具有相同的签名,该声明将失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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