简体   繁体   中英

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. Here is my declaration and call to printDeck, along with calls to other functions that all work just fine:

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. 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.

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