| | |
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:
Solved Threads: 503
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.
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.
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <ctime> #include <cstdlib> #include <vector> using namespace std; class Maze { private: char wallChar; char pathChar; char spaceChar; char startChar; char finishChar; char curPosChar; int numRows; int numCols; int curPosition[2]; int startPosition[2]; int finishPosition[2]; char** mazeGrid; vector <int[2]> correctPath; int facingDirection; public: Maze (int numrows, int numcols, char wallchar, char pathchar, char spacechar, char startchar, char finishchar, char curposchar); ~Maze (); void Maze::DisplayMaze (bool showSolution, bool showPosition, bool showDirection); void DisplayMaze (bool showSolution); void PositionAtStart (); void PositionAtFinish (); void setPosition (); bool MazeSolved (); bool Move (int newX, int newY); bool Move (int direction); bool Move (); bool Turn (bool turnRight); void GetMazePath (); }; int main () { srand (time(NULL)); Maze* maze = new Maze (21, 21, '#', '@', ' ', 'S', 'F', '?'); return 0; } Maze::Maze (int numrows, int numcols, char wallchar, char pathchar, char spacechar, char startchar, char finishchar, char curposchar) { int point[2]; point[0] = 1; point[1] = 3; correctPath.push_back (point); } Maze::~Maze () { }
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.
Well arrays cannot be copied as such.
Consider using something like
which is more meaningful than an array of 2 points, and would have proper copy semantics for the vector.
Consider using something like
C++ Syntax (Toggle Plain Text)
struct point { int x; int y; };
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
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
-chandra
•
•
Join Date: Jan 2008
Posts: 3,842
Reputation:
Solved Threads: 503
•
•
•
•
Well arrays cannot be copied as such.
Consider using something like
which is more meaningful than an array of 2 points, and would have proper copy semantics for the vector.C++ Syntax (Toggle Plain Text)
struct point { int x; int y; };
•
•
•
•
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
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.
![]() |
Similar Threads
- reallocating memory (Java)
- priority queue ordering (C++)
- help with - Read csv into vector and display elements of vector. (C++)
- Adding element to a vector crashes it? (C++)
Other Threads in the C++ Forum
- Previous Thread: a tester C++ program failing...
- Next Thread: Windows API tutorial??
Views: 1246 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linux loop looping loops map math matrix memory microsoft newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






