-
syntax problems =/
I'm making a class with various functions for finding the shortest path from one spot on a map to another around "terrain". I haven't used c++ in awhile, and I am not quite sure how to go about passing 2d char array of an unknown size into my class.
Here is just a general layout of my class so far:
Code:
class ShortPath
{
private:
struct List
{
List* node;
int f;
int h;
int g;
}*oRoot,*cRoot;
void listAdd(List* l);
public:
ShortPath(char** m);
ShortPath(char** m, char* d);
~ShortPath();
int getShortPathSteps(void);
void findShortPath(char** m);
void getShortPath(char** m, char* d);
};
How do I pass into my constructor ShortPath(char** m) a 2d array of [8][8].
ShortPath find = new ShortPath(arr2d); // doesn't work
I've tried many other things to get it to work too >.>, most it does is go in and doesn't get the even the values. I stored into arr2d. Basically what I THINK I want to do is pass in a pointer to my 2d array.
Probably a better one to structure my class, any recommendations would be awesome <3.
-
You can only pass in a 2d array if you know the second dimension at compile time. So you want vector< vector<char> >.
-
You want to rely more on std::string and std::vector. Perhaps even std::list? I do not know if it tries to use a linked list or not...
-
Code:
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
class ShortPath
{
private:
struct List
{
List* node;
int f;
int h;
int g;
}*oRoot,*cRoot;
void listAdd(List* l);//operates list recursively
public:
ShortPath(vector<vector<char> >& m);//if mem just wants to just ge
ShortPath(vector<vector<char> >& m, vector<char>& data);//if mem wants to store path too.
~ShortPath();//don't forget to free up memory.
int getShortPathSteps(void);//returns number of steps from path
void findShortPath(vector<vector<char> >& m);//does not store path. Only FINDS
void getShortPath(vector<vector<char> >& m, vector<char>& d);//finds AND stores path
};
//constructor definition(1)[user doesn't want to store path]
ShortPath::ShortPath(vector<vector<char> >& maze)
{
oRoot = NULL;
cRoot = NULL;
findShortPath(maze);
}
//constructor definition(2)[user wants to retrieve path]:
ShortPath::ShortPath(vector<vector<char> >& maze, vector<char>& data)
{
oRoot = NULL;
cRoot = NULL;
getShortPath(maze,data);
}
void ShortPath::listAdd(List* node)
{
//add method for list recursively
}
//getShortPath definition(1)[finds path only]
void ShortPath::findShortPath(vector<vector<char> >& maze)
{
int x = 0;
int y = 0;
int c = 0;
//algorithm goes here
}
void ShortPath::getShortPath(vector<vector<char> >& maze, vector<char>& data)
{
int x = 0;
int y = 0;
int c = 0;
//algorithm goes here
}
int ShortPath::getShortPathSteps(void)
{
int count = 0;
//just recurse through cRoot and count how many nodes there are
return count;
}
ShortPath::~ShortPath(){}
Pretty sure that's what you meant right? Never got proper experience with vectors... I tried creating an instance of my class and passing a 2d vector in and it seemed to work. I put a cout statement in my constructor to print out the contents of the passed in 2d vector and it worked.
can someone tell me about std::map or w/e I heard about... it sounded like something that might be of interest to me.
-
A map is an association of key to value; the keys and values may be different types. Each key can have only one value (if you want more, that's multimap); and the objects are stored so that finding a key is quick. If you're familiar with say the Perl hash, this is the same sort of deal ("associative array" is the fancy term).
-
can I do an if(maze[x][y]=='s'){ blah } if maze is a 2d vector? I keep getting a segmentation problem with the following code:
Code:
int main(int argc, char* argv[])
{
vector<vector<char> > maze(8,vector<char>(8,'x'));
ifstream maze_file("maze.txt");
int s_x=0;
int f_y=0;
int s_y=0;
int f_x=0;
int x=0;
int y=0;
if(!maze_file.is_open())
{
cout << "Error opening file!";
}
else
{
while(!maze_file.eof())
{
for(y = 0; y < 8; y++)
{
maze_file >> maze[x][y];
if(maze[x][y]=='s')
{
// s_x=x;
// s_y=y;
}
else if(maze[x][y]=='f')
{
// f_x=x;
// f_y=y;
}
}
x++;
}
}
maze_file.close();
// ShortPath find(maze,s_x,s_y);
cin.get();
return 0;
}
also it's not letting me initialize same variable types on 1 line to a value with segmenting as well =/. I don't remember it being like this. If I take out those if's it works fine.
EDIT:
nvm, figured it out... my outer loop (the while loop) was going through my maze.txt 1 row more than expected so I needed to size my vector appropriately(so it was checking a place that didn't exist).
-
I would guess that eof isn't true until you fail to read in the ninth (nonexistent) line, but you're stuck in the for loop anyway, trying to write to memory (maze[8][0]) that doesn't exist. I didn't have any problem with the four commented out lines.
Edit: The easy way, and one of the many points of vector, is that you should be using push_back instead of assignment anyway, right?
-
You can also use the .at member function to make the class throw an exception if you try to access an out-of-bounds element. Helps in debugging.