error: declaration of ‘team’ as array of references

I'm working on a program for class, and I almost have it. I just have one more error that I haven't been able to fix. I'm getting the above error in line 42, and I understand what it means, but I don't know how to fix it. The instructions for the assignment said to use reference variables, and taking away the & just gives me a different error anyway. What else could I do to fix this problem? Thanks! Here's my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
#include <iomanip>
using namespace std;
// Declare structure
struct Player
{
    char name [15];
    int jersey;
    int points;
} team[5];
// Function prototype
void displayPlayer (Player&);
int main ()
{
    for (int i=0; i<=4; i++) 
    {
        // Initialize structure
        cout << "Player " << i+1 << endl;
        cout << "Player's name: ";
        cin >> team[i].name;
        cout << "Player's jersey number: ";
        cin >> team[i].jersey;
        while (team[i].jersey<0)    // Input checking
        {
            cout << "Please enter valid input. ";
            cin >> team[i].jersey;
        }
        cout << "Points scored by player: ";
        cin >> team[i].points;
        while (team[i].points<0)    // Input checking
        {
            cout << "Please enter valid input. ";
            cin >> team[i].points;
        }
    }
    // Call function
    displayPlayer (team[5]);
    return 0;
}
void displayPlayer (Player& team [5])
{
    int sum=0;
    int max=0;
    int highest=0;
    cout << right << setw(15) << "PLAYER NAME";
    cout <<setw (15) << "JERSEY # " << setw(15) << "POINTS SCORED" << endl;
    // Display information
    for (int i=0; i<=4; i++) 
    {
        cout << setw (15) << team[i].name << setw (15) << team[i].jersey << setw(15) << team[i].points << endl;
        sum+=team[i].points;
        // Find max
        if (team[i].points>max) 
        {
            max=team[i].points;
            highest=i;
        }
    }
    // Display sum and highest scorer
    cout << "Total points scored: " << sum << endl;
    cout << "The player with the highest score is " << team[highest].name << " with a score of " << max << endl;
}
Last edited on
closed account (zb0S216C)
EricaFH wrote:
Player& team [5]

This isn't legal. To understand why, understand that a reference must be initialised when it's declared. Since there's no guarantee that an array will be initialised, there's no guarantee that all references in the array will be initialised. Furthermore, a reference isn't an object, and an array of non-existent objects doesn't make sense.

Initially, I assumed you wanted to reference an array. If I was right, then you want this:

 
void displayPlayer(Player(&team)[5]);

Here, team is a reference to an array of 5 Player objects. The use of parentheses here tells the compiler that team is a reference.

Wazzak
Last edited on
I've got it now. Thank you!
Topic archived. No new replies allowed.