C++ Overloading Extraction Operator - Error cannot access private member declared in class
Asked Answered
S

4

5

I'm working on some homework and receiving the strangest error. Hoping you can help. I am getting this error:

Cannot access private member in class

Note: I am obviously not done writing this but I try to test for errors as I go. Thanks so much for any input you have!

// Amanda 
// SoccerPlayer.cpp : main project file.
// October 6, 2012
/* a. Design a SoccerPlayer class that includes three integer fields: a player's jersey     number,
number of goals, and number of assists. Overload extraction and insertion operators for     the class.
b. Include an operation>() function for the class. One SoccerPlayer is considered greater
than another if the sum of goals plus assists is greater.
c. Create an array of 11 SoccerPlayers, then use the > operator to find the player who   has the
greatest goals plus assists.*/

#include "stdafx.h"
#include<conio.h>
#include<iostream>
#include<string>



class SoccerPlayer
{
    friend std::ostream operator<<(std::ostream, SoccerPlayer&);
//  friend std::istream operator>>(std::istream, SoccerPlayer&);
private:
    int jerseyNum;
    int numGoals;
    int numAssists;
public:
    SoccerPlayer(int, int, int);

};

SoccerPlayer::SoccerPlayer(int jersey, int goal, int assist)
{
    jerseyNum = jersey;
    numGoals = goal;
    numAssists = assist;
} 

std::ostream operator<<(std::ostream player,  SoccerPlayer& aPlayer)
{
    player << "Jersey #" << aPlayer.jerseyNum <<
        " Number of Goals " << aPlayer.numGoals << 
        " Number of Assists " << aPlayer.numAssists;
    return player ;
};

int main()
{
return 0;
} 
Silures answered 7/10, 2012 at 21:2 Comment(0)
B
2

std::ostream is not copyable. You need to pass a reference, and return a reference:

friend std::ostream& operator<<(std::ostream&, const SoccerPlayer&);

....
std::ostream& operator<<(std::ostream& player,  const SoccerPlayer& aPlayer) { /* as before */ }

Note also that there is no reason not to pass the SoccerPlayer as a const reference.

On a note completely unrelated to the error, you should prefer to use the constructor initialization list instead of assigning values to data members in the constructor body:

SoccerPlayer::SoccerPlayer(int jersey, int goal, int assist) 
: jerseyNum(jersey), numGoal(goal), numAssists(assist) {}
Barcroft answered 7/10, 2012 at 21:7 Comment(1)
I see, thanks so much! I'm having a hard time with this chapter, I really appreciate your help!Silures
L
3

You want to pass and return the streams by reference: you can't copy IOStream objects. Also, in case of the write, you probably want to pass a SoccerPlayer const&. With these changes you code should compiler (although there is also an excess semicolon after the definition of the output operator).

That is, you output operator should be declared as

std::ostream& operator<< (std::ostream&, SockerPlayer const&)

(both in its definition and the friend declaration).

Legal answered 7/10, 2012 at 21:6 Comment(0)
B
2

std::ostream is not copyable. You need to pass a reference, and return a reference:

friend std::ostream& operator<<(std::ostream&, const SoccerPlayer&);

....
std::ostream& operator<<(std::ostream& player,  const SoccerPlayer& aPlayer) { /* as before */ }

Note also that there is no reason not to pass the SoccerPlayer as a const reference.

On a note completely unrelated to the error, you should prefer to use the constructor initialization list instead of assigning values to data members in the constructor body:

SoccerPlayer::SoccerPlayer(int jersey, int goal, int assist) 
: jerseyNum(jersey), numGoal(goal), numAssists(assist) {}
Barcroft answered 7/10, 2012 at 21:7 Comment(1)
I see, thanks so much! I'm having a hard time with this chapter, I really appreciate your help!Silures
G
2

You should send the reference of ostream object to your friend function. So it will be something like friend std::ostream& operator<<(std::ostream &, SoccerPlayer&); both in prototype as well as in definition.

Galenism answered 7/10, 2012 at 21:8 Comment(0)
E
0

std::ostream operator<<(std::ostream player, SoccerPlayer& aPlayer) has to be a friend of the class or a class member to access private and protected fields.

Ethnic answered 7/10, 2012 at 21:6 Comment(2)
It is a friend. The problem is the missing copy constructor on the stream objects (it would have been helpful if the question had mentioned what the exact error was).Burnsides
@DietmarKühl: I totally missed the friend part then. Even though I looked twice.Ethnic

© 2022 - 2024 — McMap. All rights reserved.