Solved

private pointer not updating

Posted on 2000-04-06
7
173 Views
Last Modified: 2010-04-02
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
Comment
Question by:scottd1978
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 1

Expert Comment

by:ntdragon
ID: 2691373
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
 

Author Comment

by:scottd1978
ID: 2691659
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
 
LVL 3

Expert Comment

by:mnewton022700
ID: 2691772
Instead of using:

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

Try using:

per.getMap().addW(x,y);
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 3

Expert Comment

by:mnewton022700
ID: 2691774
There's no need to create a temporary map object every time.
0
 

Expert Comment

by:htcao
ID: 2692069
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
 
LVL 1

Accepted Solution

by:
ntdragon earned 50 total points
ID: 2695878
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
 

Author Comment

by:scottd1978
ID: 2696228
I solved the problem, but I'm awarding the points anyway.  I appreciate you guys helping me.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question