Solved

private pointer not updating

Posted on 2000-04-06
7
176 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

624 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