Access Member pointed to by a private pointer

I'm not sure how to word the question, so I'll update it with any suggestions.

I'm creating a Tic Tac Toe program with an attempted OO design incorporating pointers. Instead of the board holding characters ('X','O'), it instead holds objects of type Square, which each contain a pointer to a Player object.

My issue is appearing when I'm trying to overload the extraction operator and I'm getting an error saying that `name` is unaccessible.

Note: I understand that I have #pragma and #ifndef, but I switch between visual studio and g++ on linux.
Player.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#pragma once
#ifndef _PLAYER_H
#define _PLAYER_H
#include <string>
class Square;

class Player
{
	friend class Square;

public:
	/*Constructors*/
	Player();
	Player(std::string name, char gamePiece);

private:
	std::string name;
	char gamePiece;

};
#endif  


Square.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#pragma once
#ifndef _SQUARE_H
#define _SQUARE_H
#include <ostream>

class Player;

class Square
{
	friend class Player;
public:
	Square();
	Square(Player *const &player);

	friend std::ostream &operator<<(std::ostream  &output, Square &S);

private:
	Player *player;


};

#endif 

Square.cpp
1
2
3
4
5
6
7
#include "square.h"
#include "player.h"
std::ostream &operator<<(std::ostream  &output, Square &S)
{
	output << S.player->name;
	return output;
}

Last edited on
closed account (48bpfSEw)
The name of the player is "private"

1
2
private:
	std::string name;



Either you make it "public" or write a "get"- Functions which has to be public:

1
2
public:
   std::string& Name(void) { return name; }
Last edited on
I thinks its because the << operator is a friend of the Square class, but NOT a friend of the Player class. Thus, the function cannot access the private member variables of Player.
Drue Peters wrote:
Note: I understand that I have #pragma and #ifndef, but I switch between visual studio and g++ on linux.

You shouldn't need use two different include guard techniques. Both compilers support both methods.
Part of the encapsulation of your class includes the << operators. Make sure your classes friend the needed operators. You do NOT need to expose private members (don't make it public; don't write a getter).


Re: include guards: using both techniques is a cover-your-butt necessity for handling any compiler (older compilers in particular).

AFAIK, all modern (C++11 and up) compilers perform the correct handling with the #ifndef only.


Hope this helps.
> using both techniques is a cover-your-butt necessity for handling any compiler (older compilers in particular).

What compiler supports pragma but not the include guard idiom ?
Either you make it "public" or write a "get"- Functions which has to be public:


I understand the purpose accessors, but I did read they mess up escapulation like Duoas said. The goal is not to use accessors; I should have specified that in the main question.

You shouldn't need use two different include guard techniques. Both compilers support both methods.

I must have read an old article. It's been a while, but it said only Visual Studio supported pragma, but pragma tended to compile faster. Although, with how small this program is I doubt I'd notice a difference. I know this is another question, but if the majority of compilers now support pragma, what purpose is there in the #ifndef?

The issue was because << operator was not a friend to Player like y'all said, so thanks. However, I figured that since Square is friends with Player, there wouldn't be an issue. How does that work?
The << operator is not a member of the Square class so it doesn't help that Player is a friend of Square.

One possible way is to overload the << operator for the Player class as well and then use it from operator<<(std::ostream&, Square&). That should work as long as you want operator<<(std::ostream&, Square&) to output everything that operator<<(std::ostream&, Player&) does.

Otherwise you could add extra member functions (e.g. Player::printName(std::ostream&) const) that you can use. Not sure if this is better than just adding a getter function for the name though. I mean, is the name of the Player really a secret?
@a k n
You extrapolated what wasn't there. I hope Drue Peters answered you question.

TL;DR: modern compilers are equally efficient with just the #ifndef.
Topic archived. No new replies allowed.