Some help with Pointers please

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Micko1
    New Member
    • Sep 2007
    • 21

    Some help with Pointers please

    Hello,

    I am having some trouble with this function:


    Code:
    	
    void room::addConnection(string direction, room *x)
    {
    	if (direction == "north")
    	{
    		 north = x;
    	}
    }
    How can I make the connection both ways? For example I connect room1 with room2 (north) as shown above, but how can I make it that it automatically joins room2 with room1 (south)

    Thanks in advance!
  • svlsr2000
    Recognized Expert New Member
    • Feb 2007
    • 181

    #2
    Originally posted by Micko1
    Hello,

    I am having some trouble with this function:


    Code:
    	
    void room::addConnection(string direction, room *x)
    {
    	if (direction == "north")
    	{
    		 north = x;
    	}
    }
    How can I make the connection both ways? For example I connect room1 with room2 (north) as shown above, but how can I make it that it automatically joins room2 with room1 (south)

    Thanks in advance!
    Can you post more code from your file. What do you mean by joining two rooms??

    Comment

    • Micko1
      New Member
      • Sep 2007
      • 21

      #3
      Originally posted by svlsr2000
      Can you post more code from your file. What do you mean by joining two rooms??
      I have a class called "room" I have 10 rooms (room1 room2.......roo m10) the rooms are connected via pointers (North, South, East,West). I have a function that creates all the rooms, and the code above is a section from the function that connects the rooms (room1 North leads to room2 and room2 South points to room1).

      What I'm trying to achieve is a 2 way connection (North points to room2 and south points to room1)

      Below is the entire function, ive commented what I'm trying to achieve. If you need more code let me know.

      Thanks in advance :)

      Code:
      void room::addConnection(string direction, room *x) //x is next room
      {
      	if (direction == "north")
      	{
      		 north = x; //points north pointer to next room (room passed)
      		 ?          //points next room south pointer to current room
      		
      
      	}
      	else if (direction == "south")
      	{
      		 south = x; //points south pointer to next room (room passed)
      		 ?          //points next room north pointer to current room
      
      	}
      	else if (direction == "east")
      	{
      		 east = x;//points east pointer to next room (room passed)
      		 ?          //points next room west pointer to current room
      
      	}
      	else if (direction == "west")
      	{
      		 west = x;//points west pointer to next room (room passed)
      		 ?          //points next room east pointer to current room
      	}
      }

      Comment

      • Ganon11
        Recognized Expert Specialist
        • Oct 2006
        • 3651

        #4
        I would add some code in each if...else branch that calls addConnection in x (which I presume is room2) - you'll have to use the opposite direction string and the this pointer.

        Something you might run into is an infinite loop here - you set room1's connection, then call room2's connection, which in turn calls room1's connection, etc. etc. In order to fix this, I'd check to see if the current room's north/south/east/west pointer is NULL before setting the connection - if it's NULL, you're OK, but if it's not NULL, it's already been set and shouldn't be changed.

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          Originally posted by Micko1
          How can I make the connection both ways? For example I connect room1 with room2 (north) as shown above, but how can I make it that it automatically joins room2 with room1 (south)
          It looks like you need to call:
          [code=cpp]
          void room::addConnec tion(string direction, room *x)
          {
          if (direction == "north")
          {
          north = x;
          x->addConnection( "south", this);
          }
          }
          [/code]

          Do I have that right?

          Comment

          • Ganon11
            Recognized Expert Specialist
            • Oct 2006
            • 3651

            #6
            Originally posted by weaknessforcats
            It looks like you need to call:
            [code=cpp]
            void room::addConnec tion(string direction, room *x)
            {
            if (direction == "north")
            {
            north = x;
            x->addConnection( "south", this);
            }
            }
            [/code]

            Do I have that right?
            Looks right, but keep in mind the infinite loop that will result. You need to prevent that somehow.

            Comment

            • Micko1
              New Member
              • Sep 2007
              • 21

              #7
              ok cool, thankyou for the replies, I modified the fuction to check if the pointers are NULL, so it doesent keep looping :)

              I have another question:

              Code:
              int main()
              {	
              	int movesLeft = 20;
              
              	room * currentPlayerLocation;
              	room currentRoom;
              
              	currentPlayerLocation = currentRoom;
              
              	//room * currentPlayerLocation = new room ("currentRoom;");
              
              	createMaze(currentPlayerLocation);
              
              	menu(movesLeft, currentPlayerLocation);
              	
              	system ("pause");
              	return 0;
              }
              what is wrong with this?

              I keep getting this error:

              1>c:\documents and settings\comp\m y documents\visua l studio projects\room_l atest\roomtest. cpp(17) : error C2440: '=' : cannot convert from 'room' to 'room *'


              The commented section is another method I tried using, I get a fatal error.

              Thankyou once again :)

              Comment

              • Ganon11
                Recognized Expert Specialist
                • Oct 2006
                • 3651

                #8
                [CODE=cpp]
                int main()
                {
                int movesLeft = 20;

                room * currentPlayerLo cation;
                room currentRoom;

                currentPlayerLo cation = currentRoom;

                //room * currentPlayerLo cation = new room ("currentRoom;" );

                createMaze(curr entPlayerLocati on);

                menu(movesLeft, currentPlayerLo cation);

                system ("pause");
                return 0;
                }
                [/CODE]

                currentPlayerLo cation is a variable of type room* - it is a pointer to a room.

                currentRoom is a variable of type room - it is a room.

                You cannot assigm a room to a pointer. You could assign the address of a room to a pointer (currentPlayerL ocation = &currentRoom ), or the room pointed to by the pointer to a room (currentRoom = *currentPlayerL ocation), but nothing else.

                Also, I'm not sure what your room constructor expects, but I'm fairly certain it doesn't need a semicolon in there. Check what you're trying to do there and what your constructor accepts.

                Comment

                • Micko1
                  New Member
                  • Sep 2007
                  • 21

                  #9
                  Thanks for the reply, I appreciate it :)

                  Firstly, the semicolin was a typo :)

                  Even when I do as you say, I still get this error, I'm unsure what I'm getting wrong..

                  Comment

                  • Micko1
                    New Member
                    • Sep 2007
                    • 21

                    #10
                    all fixed :)

                    Thanks for all the help!

                    Comment

                    • karthickbabu
                      New Member
                      • Sep 2007
                      • 33

                      #11
                      hello

                      you use pointer variable inside the function like this.

                      north=*x;

                      U try like these


                      by
                      karthick


                      Originally posted by Micko1
                      Hello,

                      I am having some trouble with this function:


                      Code:
                      	
                      void room::addConnection(string direction, room *x)
                      {
                      	if (direction == "north")
                      	{
                      		 north = x;
                      	}
                      }
                      How can I make the connection both ways? For example I connect room1 with room2 (north) as shown above, but how can I make it that it automatically joins room2 with room1 (south)

                      Thanks in advance!

                      Comment

                      Working...