Jump to content
  • Advertisement

Archived

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

Enselic

friend cout

This topic is 5745 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
Advertisement
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
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
Guest Anonymous Poster
You sure you''re including player.h in player.cpp?

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!