C++ 指向指针的指针指向指针(错误:表达式必须具有类类型)
我在使用多层指针时遇到了一些问题。基本上,我从文件中读取点位置,并使用它们绘制多段线 我试图创建一个动态分配的数据结构,该结构将根据文件中包含的信息而变化 每个文件的结构如下所示C++ 指向指针的指针指向指针(错误:表达式必须具有类类型),c++,pointers,memory-management,dynamic,C++,Pointers,Memory Management,Dynamic,我在使用多层指针时遇到了一些问题。基本上,我从文件中读取点位置,并使用它们绘制多段线 我试图创建一个动态分配的数据结构,该结构将根据文件中包含的信息而变化 每个文件的结构如下所示 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 /
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){...}
坐标***数字点;
结构坐标{
int x;
int-y;
};
void myInit(void){…}//只包含绘图窗口的初始化内容
void loadDino(字符*文件名)
{
河道内流;
inStream.open(文件名,ios::in);//打开文件
if(inStream.fail())
返回;
闪烁的numpolys,numlines;//这些只是普通的整数
inStream>>numpolys;//读取的多边形数
//将文件中的多边形数量动态分配给数据结构
dinoPoints=新坐标**[numpolys];
对于(int j=0;j>numlines;//读取多段线中的行数
dinoPoints[j]=新坐标*[numlines];
对于(inti=0;i>恐龙点[j][i].x>>恐龙点[j][i].y;
}
}
流内关闭();
}
void myDisplay(void)
{
drawDino();//在屏幕上绘制恐龙
}
//仍在编写此函数。通过glutDisplayFunc()调用myDisplay
//并使用作为参数传递的文件名调用loadDino
void main(int argc,char**argv){…}
所以出于某种原因,它在行中给了我“表达式必须有类类型”错误
河道内>>恐龙点[j][i].x>>恐龙点[j][i].y
通常情况下,IDE(Visual Studio 2010)会在键入一个句点后显示数据结构的不同元素,但在键入“dinoPoints[j][i]后会显示。它不会显示任何可供选择的包含元素,这意味着它甚至不知道我所说的关于dinoPoints[j][i]的内容
有人知道我做错了什么吗?我觉得我在多级指针如何工作方面遗漏了一些东西,但我不确定具体是什么。您有一个三层指针。你需要三个de参考。您只有两个具有
dinoPoints[j][i]
——该表达式的结果是一个指针
更不用说你所做的可怕的不安全了。使用
向量
,这样更安全,更容易理解。您有一个三层指针。你需要三个de参考。您只有两个具有dinoPoints[j][i]
——该表达式的结果是一个指针
更不用说你所做的可怕的不安全了。使用
向量
进行此操作-更安全、更容易理解。您的dinoPoints是指针指向指针的指针,您需要3个索引才能访问坐标,而不是2个
你是C++,那你为什么不使用vector或Booo::多数组?< /p> 你的DIONIONTS是指向指针指针的指针,你需要3个索引来访问一个坐标,而不是2。
<>你在C++中,为什么不使用vector或Booo::多数组。 你的数据结构实际上看起来是一个2D数组而不是3D数组。因此,为了镜像您正在描述的数据结构,而不是数组的三个指针,您实际上只需要一个双指针或
坐标**
。这是因为坐标
变量只需要指向指针数组,每个指针指向表示多边形的坐标数组。从我所看到的,没有“多边形”类型,而是简单地将坐标数组表示为多边形。因此,这只是一个二维数组,您的dinoPoints
变量只需要指向二维数组,使其成为指向指针的指针(其中第二个指针指向动态数组),而不是三个指针
要正确分配,请执行以下操作:
步骤1)改变
到
因为您只需要一个指针数组,每个指针都指向一个坐标数组
步骤2)改变
到
现在当你调用inStream>>恐龙点[j][i].x>>恐龙点[j][i].y代码>,它应该可以正常工作
如果要尝试将dinoPoints
作为函数的引用传递,您只需要一个坐标***
类型,在该函数中,您希望更改dinoPoints
所指向的内容,并允许使用dinoPoints
的任何其他函数查看更改。。。但在本例中,它是一个全局变量,因此实际上并不需要…您的数据结构实际上是一个2D数组,而不是3D数组。因此,为了镜像您正在描述的数据结构,而不是数组的三个指针,您实际上只需要一个双指针或坐标**
。这是因为坐标
变量只需要指向指针数组,每个指针指向表示多边形的坐标数组。从我所看到的,没有“多边形”类型,而是简单地将坐标数组表示为多边形。因此,这只是一个二维数组,您的dinoPoints
变量只需要指向二维数组,使其成为指向指针的指针(其中第二个指针指向动态数组),而不是三个指针
要正确分配,请执行以下操作:
步骤1)改变
到
因为您只需要一个指针数组,该数组将包含每个poi
dinoPoints = new coordinates**[numpolys];
dinoPoints = new coordinates*[numpolys];
dinoPoints[j] = new coordinates*[numlines];
dinoPoints[j] = new coordinates[numlines];
//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]; // make an array of pointers
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;
}
}
// ** At the beginning of the file **
coordinates **dinoPoints;
// ** Inside the loadDino function **
//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]; // make an array of coordinates
for (int i = 0; i < numlines; i++) { // allocate each set of coords
// read in specific point coordinates
inStream >> dinoPoints[j][i].x >> dinoPoints[j][i].y;
}
}
inStream >> dinoPoints[j][i]->x >> dinoPoints[j][i]->y;