Archived

This topic is now archived and is closed to further replies.

Enselic

friend cout

Recommended Posts

Enselic    829
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?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Post more code, i.e. the definition of the function. It maybe doesn''t match the ''friend'' declaration.

Share this post


Link to post
Share on other sites
Enselic    829
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;
}

Share this post


Link to post
Share on other sites
civguy    308
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;

Share this post


Link to post
Share on other sites
Enselic    829
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...

Share this post


Link to post
Share on other sites
Oluseyi    2103
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.

Share this post


Link to post
Share on other sites
YoshiN    122
I know microsoft doesn''t really know how to use friend properly, so just write a simple accessor function like:

int CPlayer::getStr() const {return(m_nStr);}

and use that.

Share this post


Link to post
Share on other sites
daerid    354
You are correct, but that kind of naming does not accomplish that goal. Come back to your code in a year or so and try to figure out what variables mean, and you''ll truly understand "cryptic".

Share this post


Link to post
Share on other sites
davepermen    1047
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.

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


get visual assist and you type at most 3 letters per name.. but don''t ever type cryptic..

"take a look around" - limp bizkit
www.google.com

Share this post


Link to post
Share on other sites
Enselic    829
quote:
Original post by daerid
Come back to your code in a year or so and try to figure out what variables mean, and you''ll truly understand "cryptic".


Since I used to play DiabloII, where you never ask for Strength, you ask for str, I doubt that I will forget what Str is abbrivation for. Anyway, I _have_ commented that Str means Strenght in case i _would_ forget it, haven''t I... Overall I comment my code much.

quote:
Original post by YoshiN
I know microsoft doesn''t really know how to use friend properly, so just write a simple accessor function like:

int CPlayer::getStr() const {return(m_nStr);}

and use that.


It could be a sollution if that was one of two or three private varialbes. Since friend is made to save you from writing 20 {return m_var;} funcs, I realy want to get it to work...

Oh well, don''t stop making suggestion, That''s why I come here!


Share this post


Link to post
Share on other sites
Oluseyi    2103
Do you have the latest Service Packs? MSVC comes with an iffy friend implementation.

Don''t be cryptic when you don''t need to. It accomplishes nothing. And if saving keystrokes was so important, why are you then prepending your variable name with useless Hungarian notation?

Share this post


Link to post
Share on other sites
petewood    819
Write a public function called Output that takes an ostream and then just use that.

Then the put out function << doesn''t need to be a friend.

Even better make Output a non virtual member and make DoOutput a protected virtual function called by Output. Then you only have to specify << for the base class.

useful link

Share this post


Link to post
Share on other sites
Enselic    829
petewood: the form "object << cout;" is not acceptalbe in my code...

when I compiled civguys code (after a few slight modifications) the compiler again complined that the frined func does not have acces to the private member...


since I''ve never upgraded my MSVC++ 6.0 Enterprise edition, and since i''ve heard that microsoft aint good on following the standard, I assumed that i need to upgrade...

I made a seach on microsoft.com without any results...
only found that .NET stuff...

anyone have a link to the service pack(s) for my version (im running good ''ol win98)?

Share this post


Link to post
Share on other sites
petewood    819
sorry, i wasn't clear

ostream& operator << ( ostream& os, const CPlayer& plr )
{
return plr.output(os);
}

if you follow the link it's a good article

edit: took out the friend (copy paste mistake)


[edited by - petewood on October 22, 2002 1:00:09 PM]

Share this post


Link to post
Share on other sites