指向指针的指针(错误:表达式必须具有类类型)

发布于 2024-12-03 23:10:40 字数 2466 浏览 0 评论 0 原文

我在使用多层指针时遇到了一些问题。基本上我是从文件中读取点位置并使用它们来绘制折线。

我正在尝试创建一个动态分配的数据结构,该数据结构将根据文件中包含的信息而变化。

每个文件的结构如下。

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 ]

有谁知道我做错了什么?我觉得我错过了有关多级指针如何工作的一些东西,但我不确定到底是什么。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

北方的巷 2024-12-10 23:10:40

你有一个三层指针。您需要在那里进行三个取消引用。您只有两个 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.

一花一树开 2024-12-10 23:10:40

您的数据结构实际上看起来是 2D 数组而不是 3D 数组。因此,为了镜像您所描述的数据结构,而不是数组的三重指针,您实际上只需要一个双指针,或一个坐标**。这是因为您的坐标变量只需要指向一个指针数组,每个指针都指向一个代表多边形的坐标数组。据我所见,没有“多边形”类型,而是简单地将坐标数组表示为多边形。因此,这只是一个二维数组,您的 dinoPoints 变量只需要指向一个二维数组,使其成为一个指针到指针(其中第二个指针指向一个动态数组)数组),而不是三指针。

要正确分配它,您可以执行以下操作:

将步骤 1) 更改

dinoPoints = new coordinates**[numpolys];

dinoPoints = new coordinates*[numpolys];

,因为您只需要一个指针数组,每个指针都指向一个坐标数组。

步骤 2)

dinoPoints[j] = new coordinates*[numlines];

更改为

dinoPoints[j] = new coordinates[numlines];

当您调用 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 your coordinate 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 your dinoPoints 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

dinoPoints = new coordinates**[numpolys];

to

dinoPoints = new coordinates*[numpolys];

because you only need an array of pointers that will each point to an array of coordinates.

step 2) change

dinoPoints[j] = new coordinates*[numlines];

to

dinoPoints[j] = new coordinates[numlines];

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 pass dinoPoints as a reference to a function, where you somehow wanted to change what dinoPoints was pointing to, and allow any other function using dinoPoints to see the change ... but in this case it's a global variable, so that's not really needed...

酒中人 2024-12-10 23:10:40

您在这里使用的是指向坐标的指针数组的数组(3 个级别),但坐标数组的数组(2 个级别)就足够了。

而不是这样:

//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;

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:

//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;
    }
}

Just do:

// ** 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;
    }
}

Alternatively, if you want to stick to your original data structure, just change the line for which the error was reported to

inStream >> dinoPoints[j][i]->x >> dinoPoints[j][i]->y;
千紇 2024-12-10 23:10:40

你的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文