Hello. First post. I've just recently started learning c++ by following the tutorial on this site, mainly for reference, and the occasional google search.
Just Started messing around with classes and structs. Now I'm kind of stuck on nested classes. I think it may be more my implementation and my understanding of the concept that is giving me trouble.
#include <iostream>
usingnamespace std;
//ship class
class Ship {
public:
int Health;
bool Fighter;
bool Capital;
//Nested class of ship
class Deck{
public:
int Health,Oxygen;
bool Fire,Sealed,Venting;
}Command,Munitions,Reactor,Engines,Barracks,Hangar;
};
int main()
{
Ship MyShip;
MyShip.Fighter = true;
cout << MyShip.Fighter << endl;
MyShip.Deck.Command.Fire = true; //] Compile errors
cout << MyShip.Deck.Command.Fire; //] --------------
return 0;
}
So obviously i cant access the members of the nested class this way. So what am I doing wrong? Aren't the instances of type 'Deck' members of type 'Ship'? It seems like it should follow this type of chain that I've got going.
Also, is making the Deck class public in this case necessary? I am not sure since you are trying to access an object of it outside of the ship class...
Oh yeah of course lol! Thanks filipe.
hanst99: Yeah I set it to private just to see and no go. It needs to be public because the call to .fire in int main is outside of 'ships' and 'decks' scope.