Hitchxvi
Hitchxvi

Reputation: 3

Beginner C++, pointers and inheritance errors

I am a beginner c++ programmer well versed in csharp however..

For an assignment of mine we have to create a text based maze game, I so far have a header file which contains direction declarations and methods which set directions N,S,W,E

In the main I have gets and sets to set which direction rooms can go in. Below is examples of the code I have included one of each statements to cut the length, i.e only south get and sets.

void Room::setS(Room* nextRoom)
{
south = nextRoom;
}

Room* Room::gS(){return south;}

Room* A = new Room("A");
Room* B = new Room("B");

A->setE(B);
A->setS(E);

Room* myPosistion = A;
string location = *myPosistion->gName();

while(location !="L")
{
    cout << "You are In Room "<< location << endl;
    }

In the while loop I need to be able to access the Room::Methods, (Room::gS()), so something along the lines of

if(location->gS() == NULL
//do nothing
else 
{
  cout<<"1. South"<<endl;
}

The problem is, I cannot get the location variable to access the Room:: methods, I know im missing something very basic but just cannot get my finger on it..

Thanks for the help

Upvotes: 0

Views: 158

Answers (1)

tiridactil
tiridactil

Reputation: 389

You need to use the pointer to the object instead of the string containing the name

try something more in the lines of :

while(myPosistion->gName().compare("L") != 0 )
{
    cout << "You are In Room "<< myPosistion->gName() << endl;
    if( myPosistion->gS() != NULL )
    {
        cout << "1. South : "<< myPosistion->gS()->gName() << endl;
    }
    //TODO some processing here!
}

when you use the location variable (string), you the reference to the object

Upvotes: 1

Related Questions