Call by reference?

I think im misunderstanding call by reference?

All I need is for score_goals in the header file, when called in main to take the value of int goals and add it to the players total goals.

HEADER (implementation file)
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <iostream>
using namespace std;

// Hockey class keeping track of a player's goals, assists,
// penalties and penalty minutes
class Hockey
{
      private:
        int goals,            // number of goals
            assists,          // number of assists
            penalties,        // number of penalties
            penalty_minutes;  // total of penalty minutes
      public:
         Hockey ();
         void score_goals (Hockey player, int &goals);
         void tripping ();
         void fighting ();
         void score_goal ();
         void assist_goal ();
         void print ();
};

// Records the fact that the player has scored another goal
void Hockey::score_goal ()
{
     goals++;
}

void Hockey::score_goals (Hockey player, int &goals);
{
   int count;

   for (count = 0; count < goals; count++)
     player.score_goal ();
   cout << "\n\nPlayer in function:";
   player.print ();
   cout << "\n\n";
}


Hockey::Hockey ()
{
     goals=0;
     assists=0;
     penalties=0;
     penalty_minutes=0;
}

void Hockey::tripping ()
{
     penalties++;
     penalty_minutes++;
     penalty_minutes++;
     
}

void Hockey::fighting ()
{
     penalties++;
     penalty_minutes++;
     penalty_minutes++;
     penalty_minutes++;
     penalty_minutes++;
     penalty_minutes++;
}

void Hockey::assist_goal ()
{
     assists++;
}

// Prints the player's current statistics
void Hockey::print ()
{
     cout << "\n\nGoals:   " << goals;
     cout << "\nAssists: " << assists;
     if (penalties == 1)
       cout << "\n" << penalties << " penalty for " 
            << penalty_minutes << " minutes.";
     else
        cout << "\n" << penalties << " penalties for " 
          << penalty_minutes << " minutes.";
}



      



MAIN
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
// Main program that uses the Hockey class
#include <cstdlib>
#include <iostream>

using namespace std;

#include "Hockey.h"


void tripping ();
void fighting ();
void score_goal ();
void assist_goal ();
void print ();
void score_goals (Hockey player, int &goals);

int main(int argc, char *argv[])
{   
    Hockey Vanek, Myers;
    
    
    
    cout << "Vanek stats: ";
    Vanek.score_goals (Vanek, 7);
    Vanek.score_goal ();
    Vanek.score_goal ();
    Vanek.assist_goal ();
    Vanek.tripping ();
    Vanek.print();
    cout << endl;
    
    cout << "\n\nMyers stats: ";
    Myers.assist_goal ();
    Myers.assist_goal ();
    Myers.assist_goal ();
    Myers.fighting ();
    Myers.fighting ();
    Myers.print ();
    cout << endl;
    
    cout << "\n\n";
    system("PAUSE");
    return EXIT_SUCCESS;
}
Last edited on
where is the reference here?

Vanek.score_goals (Vanek, 7);

you declared it as reference but you do not pass it as reference!!

void score_goals (Hockey player, int &goals);

if you wanna use reference

void score_goals (Hockey player, int &goals);

with

Vanek.score_goals (Vanek, &Ivalue);

or

Vanek.score_goals (Vanek, *Ivalue);
Last edited on
You may safely ignore joneele's post as it is completely inaccurate.

A literal cannot be passed by (non-const) reference. If you were to make it pass-by-const-reference: Vanek.score_goals (Vanek, 7); would work. However, there is really no reason to pass it by reference at all.

Topic archived. No new replies allowed.