• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 183
  • Last Modified:

private pointer not updating

I have a Map class that works fine.  Inside the map class, I have MapNodes, and MapTreeNodes.  The MapTreeNodes hold MapNodes in a binary tree.  In this class I have a private variable: MapNode* current;
These nodes all have 10 pointers to MapNodes NW,N,NE,SW,S,SE,UP,DN, so this is why I only keep track of the current.  
    Now, I have another class Person that has a private member: Map myMap;
I also have a public function in person: Map& getMap();              // returns the persons map

    When I manipulate the map (using public Map functions) inside the person class, everything works fine.  But, when I use the Map& Person::getMap() function to get the map and manipulate it, something weird happens.  It keeps track of the tree fine, but the MapNode* current; variable doesn't hold its value.  I will post the outside function, let me know if you'de like to see anything else.

void move(string command, Person per, Room& old)
{
        // make sure the room has the move event
        // sounds weird eh?  why would a room NOT have the move event?
        // Don't ask me, but it could happen
        if(old.hasEvent("move"))
        {
                int x,y;
                int i = 0;
       
                // convert string to lowercase
                while(command[i])
                {
                        command[i] = tolower(command[i]);
                        i++;
                } // end convert
       
                // This if structure tests to see which direction the
                // user is trying to move.
                // I don't use a switch structure here, cause I don't
                // know how to use it with
                // strings.  If the direction is a valid direction to
                // travel in the current room
                // the room object is updated to look at the correct
                // room in the database, and
                // the person's new location is written to the database.

                // Eventually this should echo to other people's
                // terminals that the person is
                // leaving the current room.


                if(command == "n" && old.Nx() >= 0)             // North
                {
                                x = old.Nx();
                                y = old.Ny();

                                if(per.hasObject("map"))
                                {
                                        Map temp(per.getMap());
                                        temp.addN(x,y);
                                }
                }
                else if(command == "ne" && old.NEx() >= 0)      // North East
                {
                                x = old.NEx();
                                y = old.NEy();
                                if(per.hasObject("map"))
                                {

                                        Map temp(per.getMap());
                                        temp.addNE(x,y);
                                }
                }
                else if(command == "nw" && old.NWx() >= 0)      // North West
                {
                                x = old.NWx();
                                y = old.NWy();
                                if(per.hasObject("map"))
                                {

                                        Map temp(per.getMap());
                                        temp.addNW(x,y);
                                }
                }
                else if(command == "s" && old.Sx() >= 0)        // South
                {
                                x = old.Sx();
                                y = old.Sy();
                                if(per.hasObject("map"))
                                {

                                        Map temp(per.getMap());
                                        temp.addS(x,y);
                                }
                }
                else if(command == "se" && old.SEx() >= 0)      // South East
                {
                                x = old.SEx();
                                y = old.SEy();
                                if(per.hasObject("map"))
                                {

                                        Map temp(per.getMap());
                                        temp.addSE(x,y);
                                }
                }
                else if(command == "sw" && old.SWx() >= 0)      // South West
                {
                                x = old.SWx();
                                y = old.SWy();
                                if(per.hasObject("map"))
                                {
                                        Map temp(per.getMap());
                                        temp.addSW(x,y);
                                }
                }
                else if(command == "e" && old.Ex() >= 0)        // East
                {
                                x = old.Ex();
                                y = old.Ey();
                                if(per.hasObject("map"))
                                {
                                        Map temp(per.getMap());
                                        temp.addE(x,y);
                                }
                }
                else if(command == "w" && old.Wx() >= 0)        // West
                {
                                x = old.Wx();
                                y = old.Wy();
                                if(per.hasObject("map"))
                                {
                                        Map temp(per.getMap());
                                        temp.addW(x,y);
                                }
                }
                else if((command == "u" || command == "up") && old.UPx() >= 0)  // Up
                {
                                x = old.UPx();
                                y = old.UPy();
                                if(per.hasObject("map"))
                                {
                                        // Map doesn't understande up and down yet
                                }
                }
                else if((command == "d" || command == "dn") && old.DNx() >= 0) // Down
                {
                                x = old.DNx();
                                y = old.DNy();
                                if(per.hasObject("map"))
                                {
                                        // Map doesn't understande up and down yet
                                }
                }

                // If the room requested is not an adjecent room, let the user know
                else
                {
                        cout << "There is no obvious exit there." << endl;
                        return;
                }

                // reset the coordinates of the room
                old.setCoord(x,y);

                // Call look function

                // Set person room x and y
                per.setRoom(x,y);

                // In the future, this is where we should send a message to
                // everyone in the current room showing that the current player
                // left the room

                // sendmesg(Room,Person,"Left the room ",string );
        }
        else
                cout << "no move allowed for room " << old.getRoomX() << "," << old.getRoomY() << endl;

}
0
scottd1978
Asked:
scottd1978
  • 2
  • 2
  • 2
  • +1
1 Solution
 
ntdragonCommented:
i didn't understand what problem did you have and what exectlly did you try to do

but try first to make the MapNode* current public and try then
tell me if it helped if it did i think i know what you can do

if not try to explain the error and what exectlly did you do
0
 
scottd1978Author Commented:
That doesn't do the trick.

I have a class Person that has a private member Map.  I want to update that map from an outside function (move from above).  I am currently setting a Map = to the map in the Person class using the Map& Person::getMap() function.  Now, when I call Map functions that add nodes, the nodes get added to the map in the person class.  The problem is that the current variable doesn't seem to get updated.  Whenever a node is added, the current variable, which is a pointer to a MapNode, doesn't seem to get updated.  I thought that pointer were always pass by reference.


I hope that makes sense. :-)
0
 
mnewton022700Commented:
Instead of using:

Map temp(per.getMap());
temp.addW(x,y)

Try using:

per.getMap().addW(x,y);
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
mnewton022700Commented:
There's no need to create a temporary map object every time.
0
 
htcaoCommented:
Your code:

Map temp(per.getMap());

creates a new "temp" object with a reference to a map object "per.getMap()" passed into its constructor.  I think what you're trying to do is

Map &temp = per.getMap();
0
 
ntdragonCommented:
first pointers go byval not by ref but it the same when you send something byref it like you sended a pointer to it

if what i asked you to do didn't help
try to change your func Map& getMap();
from byref to byval i mean try
Map getMap();
<don't worry about the fact that it will be slawer if that the problem i"ll help you to fix it and still use byref
but first we have to find the problem>

if it won't help send the reset of your code
0
 
scottd1978Author Commented:
I solved the problem, but I'm awarding the points anyway.  I appreciate you guys helping me.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 2
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now