2012-10-07 10 views
5

Sto lavorando su alcuni compiti e sto ricevendo l'errore più strano. Spero che tu possa aiutare. Sto ottenendo questo errore:Operatore di estrazione da sovraccarico C++: l'errore non può accedere a un membro privato dichiarato nella classe

Cannot access private member in class

Nota: Non sto ovviamente fatto scrivere questo ma cerco di prova per gli errori come vado. Grazie mille per ogni input che hai!

// 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; 
} 

risposta

2

std::ostream non è possibile copiare. È necessario passare un riferimento, e restituire un riferimento:

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

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

Si noti anche che non v'è alcun motivo per non passare il SoccerPlayer come riferimento const.

In una nota del tutto estranei a l'errore, si dovrebbe preferire di usare la lista di inizializzazione del costruttore invece di assegnare valori ai membri di dati nel corpo del costruttore:

SoccerPlayer::SoccerPlayer(int jersey, int goal, int assist) 
: jerseyNum(jersey), numGoal(goal), numAssists(assist) {} 
+0

Vedo, grazie mille! Sto attraversando un periodo difficile con questo capitolo, apprezzo molto il tuo aiuto! –

3

Si desidera passare e restituire i flussi in base al riferimento: non è possibile copiare oggetti IOStream. Inoltre, in caso di scrittura, probabilmente vuoi passare un SoccerPlayer const&. Con queste modifiche il codice dovrebbe compilare (anche se c'è anche un punto e virgola in eccesso dopo la definizione dell'operatore di output).

Cioè, è operatore di output dovrebbe essere dichiarato come

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

(sia nella sua definizione e la dichiarazione friend).

0

std::ostream operator<<(std::ostream player, SoccerPlayer& aPlayer) deve essere un amico della classe o un membro della classe per accedere ai campi private e protected.

+1

È un "amico". Il problema è il costruttore di copie mancante sugli oggetti del flusso (sarebbe stato utile se la domanda avesse indicato quale fosse l'errore esatto). –

+0

@ DietmarKühl: Ho perso totalmente la parte amica di allora. Anche se ho guardato due volte. – JimR

2

è necessario inviare il riferimento dell'oggetto ostream a la tua funzione amico Quindi sarà qualcosa come friend std::ostream& operator<<(std::ostream &, SoccerPlayer&); sia in prototipo che in definizione.

Problemi correlati