friend cout

Started by
20 comments, last by Enselic 21 years, 5 months ago
i''ve made this declaration in class CPlayer: friend ostream& operator << ( ostream& os, const CPlayer& plr ); within the defination of that func i do this: os << endl << "Name: " << plr.m_szName; and i get this error: c:\övrigt\c++\adclo\player.cpp(204) : error C2248: ''m_szName'' : cannot access private member declared in class ''CPlayer'' and i CANT get why i do get that error!! anyone can help me?
[s]--------------------------------------------------------[/s]chromecode.com - software with source code
Advertisement
Post more code, i.e. the definition of the function. It maybe doesn''t match the ''friend'' declaration.
player.h:

  /*the player class*/#ifndef PLAYER_H#define PLAYER_H#include "item.h"#include <iostream>#include <fstream>#include <cmath>using namespace std;static const int MAX_NAME_LENGTH = 20;class CPlayer {public:	...	friend ostream& operator<<( ostream& os, const CPlayer& plr );	...private:	// "#defines" for the items	enum  { ARMOR = 0,		    HELM,		    SHIELD,		    WEAPON,		    RING,		    AMULET };	char	m_szName[MAX_NAME_LENGTH];	long	m_nLevel;	int		m_nHealth;	int		m_nMaxHealth;	int		m_nMinDmg;		// minimum damage	int		m_nMaxDmg;		// maxmum damage	long	m_nDef;			// defense	long	m_nAR;			// attack rating		int		m_nStr;			// strength	int		m_nTech;		// technique	int		m_nVit;			// vitality	int		m_nInt;			// intelligence	long	m_nXP;			// experience	long	m_nNextLevel;	// xp needed to reach next level	long	m_nGameLevel;	// how far the player has come	long m_nGold;	CItem m_Items[ 6 ];};#endif  


part of player.cpp

  ostream& operator<<( ostream& os, const CPlayer& plr ){	os << endl << "CHARCTER STATS AND INFO";	os << endl << "-----------------------";	os << endl << "Name:         " << plr.m_szName;	os << endl << "Level:        " << plr.m_nLevel;	os << endl << "";	os << endl << "Health:       " << plr.m_nHealth << " / " << m_nMaxHealth;	os << endl << "Defense:      " << plr.m_nDef;	os << endl << "Damage:       " << plr.m_nMinDmg << " - " << m_nMaxDmg;	os << endl << "Attack rating " << plr.m_nAR;	os << endl << "";	os << endl << "Strength:     " << plr.m_nStr;	os << endl << "Technique:    " << plr.m_nTech;	os << endl << "Vitality:     " << plr.m_nVit;	os << endl << "Intelligence  " << plr.m_nInt;	os << endl << "";	os << endl << "Experience:   " << plr.m_nXP;	os << endl << "Next Level:   " << plr.m_nNextLevel;	os << endl << "";	os << endl << "Gold:         " << plr.m_nGold;	os << endl << "";	os << endl << "";	os << endl << "";	os << endl << "";	cin.get();	return os;}  
[s]--------------------------------------------------------[/s]chromecode.com - software with source code
c''mon SOMEONE must know the prob, right?
[s]--------------------------------------------------------[/s]chromecode.com - software with source code
You sure you''re including player.h in player.cpp?
make it public and try it.
Domine non secundum peccata nostra facias nobis
All I have to say is that this compiles fine:

main.cpp

  #include <iostream>#include <stdio.h>#include "player.h"int main(int argc, char *argv[]){  CPlayer player(5);  std::cout << player;  getchar();  return 0;}  


player.cpp

  #include <iostream>#include "player.h"using namespace std;ostream& operator<<( ostream& os, const CPlayer& plr ){  os << endl << "CHARCTER STATS AND INFO";  os << endl << "-----------------------";  os << endl << "Name:         " << plr._id;  return os;}  


player.h

  #ifndef PLAYER_H#define PLAYER_H#include <iosfwd>class CPlayer {public:  friend ostream& operator<<( ostream& os, const CPlayer& plr );  CPlayer(int id) : _id(id) {}private:  int _id;};#endif  

Well, try compiling something simpler like that and you''re bound to find the bug eventually .

As a sidenote, name your variables better. Why have
int m_nStr; // strength

when you could have
int m_nStrength;

?

Or even forget about some of the hungarian stuff:

int m_strength;
quote:Original post by civguy
As a sidenote, name your variables better. Why have
int m_nStr; // strength

when you could have
int m_nStrength;

?


simply cause m_nStr can be written alot faster than m_nStrength. i had nStrength fist but abbriviated it to nStr.

anyway, i''ll let you know if i solve the problem...

[s]--------------------------------------------------------[/s]chromecode.com - software with source code
quote:Original post by Enselic
simply cause m_nStr can be written alot faster than m_nStrength. i had nStrength fist but abbriviated it to nStr.

aka, meaninglessly cryptic.
the most important is that it is not cryptic to the programmer i.e. me...
[s]--------------------------------------------------------[/s]chromecode.com - software with source code

This topic is closed to new replies.

Advertisement