473,395 Members | 1,996 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Some help with Pointers please

21
Hello,

I am having some trouble with this function:


Expand|Select|Wrap|Line Numbers
  1.  
  2. void room::addConnection(string direction, room *x)
  3. {
  4.     if (direction == "north")
  5.     {
  6.          north = x;
  7.     }
  8. }
  9.  
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!
Oct 22 '07 #1
10 1293
svlsr2000
181 Expert 100+
Hello,

I am having some trouble with this function:


Expand|Select|Wrap|Line Numbers
  1.  
  2. void room::addConnection(string direction, room *x)
  3. {
  4.     if (direction == "north")
  5.     {
  6.          north = x;
  7.     }
  8. }
  9.  
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??
Oct 22 '07 #2
Micko1
21
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.......room10) 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 :)

Expand|Select|Wrap|Line Numbers
  1. void room::addConnection(string direction, room *x) //x is next room
  2. {
  3.     if (direction == "north")
  4.     {
  5.          north = x; //points north pointer to next room (room passed)
  6.          ?          //points next room south pointer to current room
  7.  
  8.  
  9.     }
  10.     else if (direction == "south")
  11.     {
  12.          south = x; //points south pointer to next room (room passed)
  13.          ?          //points next room north pointer to current room
  14.  
  15.     }
  16.     else if (direction == "east")
  17.     {
  18.          east = x;//points east pointer to next room (room passed)
  19.          ?          //points next room west pointer to current room
  20.  
  21.     }
  22.     else if (direction == "west")
  23.     {
  24.          west = x;//points west pointer to next room (room passed)
  25.          ?          //points next room east pointer to current room
  26.     }
  27. }
Oct 22 '07 #3
Ganon11
3,652 Expert 2GB
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.
Oct 22 '07 #4
weaknessforcats
9,208 Expert Mod 8TB
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:
Expand|Select|Wrap|Line Numbers
  1. void room::addConnection(string direction, room *x)
  2. {
  3.     if (direction == "north")
  4.     {
  5.          north = x;
  6.          x->addConnection("south", this);
  7.     }
  8. }
  9.  
Do I have that right?
Oct 22 '07 #5
Ganon11
3,652 Expert 2GB
It looks like you need to call:
Expand|Select|Wrap|Line Numbers
  1. void room::addConnection(string direction, room *x)
  2. {
  3.     if (direction == "north")
  4.     {
  5.          north = x;
  6.          x->addConnection("south", this);
  7.     }
  8. }
  9.  
Do I have that right?
Looks right, but keep in mind the infinite loop that will result. You need to prevent that somehow.
Oct 22 '07 #6
Micko1
21
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:

Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {    
  3.     int movesLeft = 20;
  4.  
  5.     room * currentPlayerLocation;
  6.     room currentRoom;
  7.  
  8.     currentPlayerLocation = currentRoom;
  9.  
  10.     //room * currentPlayerLocation = new room ("currentRoom;");
  11.  
  12.     createMaze(currentPlayerLocation);
  13.  
  14.     menu(movesLeft, currentPlayerLocation);
  15.  
  16.     system ("pause");
  17.     return 0;
  18. }
  19.  
what is wrong with this?

I keep getting this error:

1>c:\documents and settings\comp\my documents\visual studio projects\room_latest\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 :)
Oct 23 '07 #7
Ganon11
3,652 Expert 2GB
Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {    
  3.     int movesLeft = 20;
  4.  
  5.     room * currentPlayerLocation;
  6.     room currentRoom;
  7.  
  8.     currentPlayerLocation = currentRoom;
  9.  
  10.     //room * currentPlayerLocation = new room ("currentRoom;");
  11.  
  12.     createMaze(currentPlayerLocation);
  13.  
  14.     menu(movesLeft, currentPlayerLocation);
  15.  
  16.     system ("pause");
  17.     return 0;
  18. }
  19.  
currentPlayerLocation 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 (currentPlayerLocation = &currentRoom), or the room pointed to by the pointer to a room (currentRoom = *currentPlayerLocation), 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.
Oct 23 '07 #8
Micko1
21
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..
Oct 24 '07 #9
Micko1
21
all fixed :)

Thanks for all the help!
Oct 24 '07 #10
hello

you use pointer variable inside the function like this.

north=*x;

U try like these


by
karthick


Hello,

I am having some trouble with this function:


Expand|Select|Wrap|Line Numbers
  1.  
  2. void room::addConnection(string direction, room *x)
  3. {
  4.     if (direction == "north")
  5.     {
  6.          north = x;
  7.     }
  8. }
  9.  
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!
Oct 24 '07 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Tommy Lang | last post by:
I am working on this project and I need some help/pointers/comments to get me started, I am stuck. The program will be used to store information in an array while it is running. I need to store...
2
by: Super KHB | last post by:
I am still trying to display 2D arrays with pointers. My prof gave us the prototype void DisplayArray (int (*); where inside the function DisplayArray(array) *(*(array + row) + col) would...
2
by: LeTubs | last post by:
Hi I'm think I'm having some problem with pointers..... namely passing them around...Or I think ? I've used gdb to find the following (note conn is of type MYSQL *conn). Program received...
11
by: Peter Mount | last post by:
Hello Are there any good online tutorials that explain pointers in ANSI C? I've covered pointers in my course but I'm just having trouble "getting it to sink in". Thanks Peter Mount...
5
by: Paminu | last post by:
Why make an array of pointers to structs, when it is possible to just make an array of structs? I have this struct: struct test { int a; int b;
1
by: Tiago | last post by:
I´m doing a paper about Binary Tree with pointers, and a want to reproduce them in C# but I must use pointers .I Tried to do but failed . My piece of code (with pointers) not Working(I can´t use...
4
by: pcnate | last post by:
I've been having some problems with pointers and such.This is homework, so I don't want people writing codeand telling me to use it. I just want some direction on what isn't working. here is...
5
by: Y2J | last post by:
I am working through this book on C++ programming, the author is speaking of using linked lists. He gave and example which I found confusing to say the least. So I rewrote the example in a way that...
10
by: arnuld | last post by:
thanks
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.