Menu

Global struct/array

2002-12-10
2012-09-26
  • Nobody/Anonymous

    I have a function that reads vertices from a file into a struct/array. I then need to get those numbers to my graphics function. However, when I try to declare a global struct/array with a variable size-type the compiler complains. But, if I put it in a function, I get the usual "undeclared" error. So, how do I declare a global struct/array? Code:

    struct _Verts {
    float X;
    float Y;
    float Z;
    } VERTEX [Verts];

    Trevor

     
    • Derek Baker

      Derek Baker - 2002-12-10

      struct Verts {
      float X;
      float Y;
      float Z;
      };

      Verts VERTEX;

      Creates an instance of Verts called VERTEX. Put it before main.

      Derek

       
    • Nobody/Anonymous

      Wow, thanks man! I asked 3 experts that same question and they all drew blanks.

      Trevor

       
    • Nobody/Anonymous

      However, it doesn't do what I want :/

      I use the struct/array to hold verts, so at run-time the number of arrays to make (each holding a struct) is then decided. Then it's quite simple to access each struct in the array. But, if I decalre it as a gloabal variable it tries to create a NULL number of array's, thus it refuses to compile. If I make it local, then I can't use it in other functions. Code:

      struct _Verts {
      float X;
      float Y;
      float Z;
      } VERTEX [Verts]; //Where Verts is the number of arrays to make

      To access each struct:
      Verts[Vert].X //Where Vert is the respective array to access from 0 to Verts

       
    • Nobody/Anonymous

      The code i wrote below works fine.  I think you were getting errors because "VERTS" has to be a constant.

      #include<iostream>
      #include<math.h>
      #include<stdlib.h>
      using namespace std;
      const int VERTS = 314;

      struct _Verts {
      float X;
      float Y;
      float Z;
      } VERTEX [VERTS]; //Where VERTS is the number of arrays to make

      int main(){
          for(int i=0; i<VERTS; i++){
              float theta = static_cast<float>(i)/100.0;
              VERTEX[i].X = cos(theta);
              VERTEX[i].Y = sin(theta);
              VERTEX[i].Z = 0.0;
              cout    << '('
                      << VERTEX[i].X << ", "
                      << VERTEX[i].Y << ", "
                      << VERTEX[i].Z
                      << ')'
                      << '\n'; 
          }
          cout << endl;
          system("PAUSE");
          return 0;
      }

      =-)
      Nhan

       
    • Nobody/Anonymous

      Exactly, it needs to be a const, but I don't want it to be a const :/ The number of VERTEX's changes per file, so it can't be a const. :/

      Trevor

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.