I’m having some trouble with multi-layer pointers. Basically I’m reading point locations from a file and using them to map out polylines.
I’m trying to create a dynamically allocated data structure that will change depending on the information contained in the file.
Each file is structured like this.
29 // number of polylines in the whole file
3 // first polyline, number of points in it
32 435 // first coordinate where x = 32 and y = 435
15 200
100 355
10 // second polyline, number of points in it
245 35
330 400
and so on etc.
I created a struct with x and y ints to hold the coordinates for each point
struct coordinates{
int x;
int y;
};
I want to basically create a data structure like this …
Pointer --> array w/ num of polys
| | |
| | |
v v v
poly0 poly1 poly2 // arrays with coordinate structs
x1,y1 x1,y1 x1,y1
x2,y2 x2,y2 x2,y2
x3,y3 x3,y3 x3,y3
Here’s what my code looks like
coordinates *** dinoPoints;
struct coordinates{
int x;
int y;
};
void myInit(void){...} // just has initialization stuff for the draw window
void loadDino (char * fileName)
{
fstream inStream;
inStream.open(fileName, ios ::in); // open the file
if(inStream.fail())
return;
GLint numpolys, numlines; // these are just regular ints
inStream >> numpolys; //reads in number of polys
//dynamically allocates the number of polys in file to datastructure
dinoPoints = new coordinates**[numpolys];
for(int j = 0; j < numpolys; j++){ // read each polyline
inStream >> numlines; // read in number of lines in polyline
dinoPoints[j] = new coordinates*[numlines];
for (int i = 0; i < numlines; i++){ // allocate each set of coords
dinoPoints[j][i] = new coordinates;
// read in specific point coordinates
inStream >> dinoPoints[j][i].x >> dinoPoints[j][i].y;
}
}
inStream.close();
}
void myDisplay(void)
{
drawDino(); // draws the dinosaur on the screen
}
//still writing this function. Calls myDisplay through glutDisplayFunc()
// and also calls loadDino with filename passed as a parameter
void main(int argc, char **argv){...}
So for some reason it’s giving me “expression must have classtype” errors on the line
inStream >> dinoPoints[ j ][ i ].x >> dinoPoints[ j ][ i ].y;
Also normally the IDE (Visual Studios 2010) will show the different elements of a data structure after a period is typed, but after typing “dinoPoints[ j ][ i ]. ” it doesn’t show up with any contained elements to select from, which means it doesn’t even know what I’m talking about in regards to dinoPoints[ j ][ i ]
Does anyone know what I’m doing wrong? I feel like i’m missing something in regards to how the multilevel pointers work, but I’m not sure exactly what.
You’ve got a triple-layered pointer. You need three de-references in there. You’ve only got two with
dinoPoints[j][i]– the result of that expression is a pointer.Not to mention the horrendous unsafety of what you’re doing. Use a
vector<vector<vector<coordinates>>>for this- it’s safer and easier to understand.