指向指针的指针(错误:表达式必须具有类类型)
我在使用多层指针时遇到了一些问题。基本上我是从文件中读取点位置并使用它们来绘制折线。
我正在尝试创建一个动态分配的数据结构,该数据结构将根据文件中包含的信息而变化。
每个文件的结构如下。
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
我创建了一个带有
x 和 y 整数的结构来保存每个点的坐标
struct coordinates{
int x;
int y;
};
我想基本上创建一个像这样的数据结构...
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
这是我的代码的样子
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){...}
所以出于某种原因它给了我“表达式必须” 行上有 classtype" 错误
inStream >> dinoPoints[ j ][ i ].x >>> dinoPoints[ j ][ i ].y;
通常情况下,IDE (Visual Studios 2010) 会在键入句点后显示数据结构的不同元素,但在键入“dinoPoints[ j ][ i ].”后,它不会显示任何包含的元素可供选择,这意味着它甚至不知道我在谈论 dinoPoints[ j ][ i ]
有谁知道我做错了什么?我觉得我错过了有关多级指针如何工作的一些东西,但我不确定到底是什么。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你有一个三层指针。您需要在那里进行三个取消引用。您只有两个
dinoPoints[j][i]
- 该表达式的结果是一个指针。更不用说你正在做的事情存在可怕的不安全性。为此,请使用
vector>>
- 它更安全、更容易理解。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.您的数据结构实际上看起来是 2D 数组而不是 3D 数组。因此,为了镜像您所描述的数据结构,而不是数组的三重指针,您实际上只需要一个双指针,或一个坐标**。这是因为您的坐标变量只需要指向一个指针数组,每个指针都指向一个代表多边形的坐标数组。据我所见,没有“多边形”类型,而是简单地将坐标数组表示为多边形。因此,这只是一个二维数组,您的
dinoPoints
变量只需要指向一个二维数组,使其成为一个指针到指针(其中第二个指针指向一个动态数组)数组),而不是三指针。要正确分配它,您可以执行以下操作:
将步骤 1) 更改
为
,因为您只需要一个指针数组,每个指针都指向一个
坐标
数组。步骤 2)
更改为
当您调用
inStream >> 时 Now dinoPoints[j][i].x >>> dinoPoints[j][i].y;
,它应该可以正常工作。如果您尝试将
dinoPoints
作为对函数的引用传递,并且您想以某种方式更改dinoPoints,那么您只需要一个
指向,并允许使用cooperative***
类型dinoPoints
的任何其他函数查看更改...但在本例中它是一个全局变量,因此实际上并不需要...Your data-structure actually appears to be a 2D-array rather than a 3D-array. Thus in order to mirror the data-structure you're describing, rather than a triple-pointer for your array, you only really need a double pointer, or a
coordinate**
. This is because yourcoordinate
variable only needs to point to an array of pointers that each point to an array of coordinates that represent your polygons. From what I can see, there is no "polygon" type, but rather you are simply representing an array of coordinates as a polygon. Thus, this is only a two-dimentional array, and yourdinoPoints
variable only needs to point to a 2-dimensional array, making it a pointer-to-pointer (where the second pointer points to a dynamic array), not a triple-pointer.To allocate this properly, you would do the following:
step 1) change
to
because you only need an array of pointers that will each point to an array of
coordinates
.step 2) change
to
Now when you call
inStream >> dinoPoints[j][i].x >> dinoPoints[j][i].y;
, it should work correctly.You would only need a
coordinate***
type if you were to try and passdinoPoints
as a reference to a function, where you somehow wanted to change whatdinoPoints
was pointing to, and allow any other function usingdinoPoints
to see the change ... but in this case it's a global variable, so that's not really needed...您在这里使用的是指向坐标的指针数组的数组(3 个级别),但坐标数组的数组(2 个级别)就足够了。
而不是这样:
只需这样做:
或者,如果您想坚持原来的数据结构,只需更改报告错误的行即可
You're using an array of arrays of pointers to coordinates here (3 levels), but an array of arrays of coordinates (2 levels) would suffice.
Instead of this:
Just do:
Alternatively, if you want to stick to your original data structure, just change the line for which the error was reported to
你的 dinoPoints 是一个指向指针的指针,你需要 3 个索引来访问坐标,而不是 2 个。
你使用的是 C++,所以为什么不使用向量或 boost::multiarray。
Your dinoPoints is a pointer to pointer to pointer, you need 3 indices to access a coordinate, not 2.
You are in C++ so why don't you use vector, or boost::multiarray.