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
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.
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.
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?