Trying to push 2-element array onto vector

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jan 2008
Posts: 3,842
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Trying to push 2-element array onto vector

 
0
  #1
Jan 12th, 2009
I am developing a program that creates random mazes. One part of the program involves keeping track of the correct path. Each point on the path is a (row, column) coordinate. I am trying to store each coordinate as a 2 element integer array and push it back onto a vector of all the coordinates in the correct path, but I'm not having much success. Here's the code, stripped down to get right to the error, which is in line 58.

  1. #include <iostream>
  2. #include <ctime>
  3. #include <cstdlib>
  4. #include <vector>
  5. using namespace std;
  6.  
  7.  
  8. class Maze
  9. {
  10. private:
  11. char wallChar;
  12. char pathChar;
  13. char spaceChar;
  14. char startChar;
  15. char finishChar;
  16. char curPosChar;
  17. int numRows;
  18. int numCols;
  19. int curPosition[2];
  20. int startPosition[2];
  21. int finishPosition[2];
  22. char** mazeGrid;
  23. vector <int[2]> correctPath;
  24. int facingDirection;
  25.  
  26.  
  27. public:
  28. Maze (int numrows, int numcols, char wallchar, char pathchar, char spacechar, char startchar, char finishchar, char curposchar);
  29. ~Maze ();
  30. void Maze::DisplayMaze (bool showSolution, bool showPosition, bool showDirection);
  31. void DisplayMaze (bool showSolution);
  32. void PositionAtStart ();
  33. void PositionAtFinish ();
  34. void setPosition ();
  35. bool MazeSolved ();
  36. bool Move (int newX, int newY);
  37. bool Move (int direction);
  38. bool Move ();
  39. bool Turn (bool turnRight);
  40. void GetMazePath ();
  41. };
  42.  
  43.  
  44. int main ()
  45. {
  46. srand (time(NULL));
  47. Maze* maze = new Maze (21, 21, '#', '@', ' ', 'S', 'F', '?');
  48. return 0;
  49. }
  50.  
  51.  
  52. Maze::Maze (int numrows, int numcols, char wallchar, char pathchar, char spacechar, char startchar, char finishchar, char curposchar)
  53. {
  54. int point[2];
  55. point[0] = 1;
  56. point[1] = 3;
  57.  
  58. correctPath.push_back (point);
  59. }
  60.  
  61.  
  62. Maze::~Maze ()
  63. {
  64. }

So the questions are:

1) What am I doing wrong?
2) Is it possible to store the coordinates this way (a vector of 2-element arrays) and if so, what do I need to change?
3) If not, do I need to create a Coordinate class that has the 2 element array as the lone data member and have a vector of Coordinate? Or should I use http://www.cplusplus.com/reference/s...lity/pair.html ?

Thank you.
Last edited by VernonDozier; Jan 12th, 2009 at 1:44 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Trying to push 2-element array onto vector

 
0
  #2
Jan 12th, 2009
Well arrays cannot be copied as such.

Consider using something like
  1. struct point {
  2. int x;
  3. int y;
  4. };
which is more meaningful than an array of 2 points, and would have proper copy semantics for the vector.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 462
Reputation: Agni is a jewel in the rough Agni is a jewel in the rough Agni is a jewel in the rough Agni is a jewel in the rough 
Solved Threads: 71
Sponsor
Agni's Avatar
Agni Agni is offline Offline
Posting Pro in Training

Re: Trying to push 2-element array onto vector

 
0
  #3
Jan 12th, 2009
1> i dont know if this will work(i mean the vector thing, not struct way). but assuming it does,
one issue i see with using a vector is if you have to verify that a particular point is on the right path or not. Suppose you have a maze and someone is trying to go through it and you need to verify that he's on the right path how do you do that? do you fetch each element which is an array and then compare each element of that array with the present co-ordinates? That would be pretty slow.
3> May be we can create a string of x-y coordinates.
row = 2, col =3
create a string "0203" and store strings in vectors, comparison might also be easier.
4> All these are as per my understanding of the problem
Last edited by Agni; Jan 12th, 2009 at 2:36 am.
thanks
-chandra
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,842
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Trying to push 2-element array onto vector

 
0
  #4
Jan 12th, 2009
Originally Posted by Salem View Post
Well arrays cannot be copied as such.

Consider using something like
  1. struct point {
  2. int x;
  3. int y;
  4. };
which is more meaningful than an array of 2 points, and would have proper copy semantics for the vector.
Good idea. That's what I'll do. Thank you.

Originally Posted by Agni View Post
1> i dont know if this will work(i mean the vector thing, not struct way). but assuming it does,
one issue i see with using a vector is if you have to verify that a particular point is on the right path or not. Suppose you have a maze and someone is trying to go through it and you need to verify that he's on the right path how do you do that? do you fetch each element which is an array and then compare each element of that array with the present co-ordinates? That would be pretty slow.
3> May be we can create a string of x-y coordinates.
row = 2, col =3
create a string "0203" and store strings in vectors, comparison might also be easier.
4> All these are as per my understanding of the problem
I have a 2-dimensional character array called mazeGrid in addition to the vector. It has different characters for walls, spaces where you can walk, but which are not on the correct path, and spaces where you can walk which are on the correct path, so given any coordinate pair (say (3,5)), you can immediately tell whether it is a wall, part of the path, or part of a dead end by simply checking mazeGrid[3][5] . The vector is intended to be a redundant storage of the correct path coordinates so that you don't have to do a brute force scanning of the 2-D array mazeGrid to find the path if you didn't know it already (by "you", I mean the programmer, not the player). The game may have a feature where you can click a button to display the actual route though depending on the level the player chooses.

Thanks for the input.
Last edited by VernonDozier; Jan 12th, 2009 at 6:27 am.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 1246 | Replies: 3
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC