Thread: Vector of structures

  1. #1
    The Reel Thing
    Join Date
    Jun 2005
    Posts
    44

    Vector of structures

    Hello, I'm currently writing a model loader, and I have quite a few structures for different things, vertices, normals, materials, that stuff. Anyways, in my main model class, I want to have vectors to hold many of those structures. I don't quite know how to explain it, but here's what I mean in code:

    Code:
    typedef struct
    {
       USHORT flag;
       USHORT vertex_index1, vertex_index2, vertex_index3;
       USHORT normal_index1, normal_index2, normal_index3;
       USHORT smothing_group;
    } TRIANGLE;
    
    class Model
    {
       public:
          ..Yadda yadda yada..
       private:
          vector<TRIANGLE> triangles;
    }
    As you can see, I can access all the triangles in the model from that one vector. I like that idea, because I can allocate the space as needed, and it's more encapsulated like that.

    Anyways, on to my problem. How would I initialize these, add another triangle, etc? Is this even possible?

    Thanks in advance
    Bagpipes – putting the fun back in funeral.

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Of course it's possible. It is also eased by the addition of C++ struct constructors (making structs nearly (?) identical to classes). Here's a quick example working with your class. Maybe some ideas. Maybe some very very messy code too

    Code:
    #include <vector>
    #include <iostream>
    typedef unsigned short USHORT;
    typedef struct Triangle
    {
       USHORT flag;
       USHORT something_group;
       Triangle()
       {
    	   flag = 0;
    	   something_group = 0;
       }
       Triangle(int a, int b)
       {
    	   flag = a;
    	   something_group = b;
       }
    } TRIANGLE;
    
    class Model
    {
       public:
    	   void AddTriangle() {
    		   triangles.push_back(Triangle());
    	   }
    	   void AddTriangle(int flag, int something_group) {
    		   triangles.push_back(Triangle(flag, something_group));
    	   }
    	   TRIANGLE GetTriangle(int i) {
    		   return triangles[i];
    	   } 
       private:
    	   std::vector<TRIANGLE> triangles;
    };
    
    int main(int argc, char *argv[])
    {
    	Model model;
    	model.AddTriangle();
    	model.AddTriangle(5, 6);
    	std::cout << model.GetTriangle(1).flag;
    	return 0;
    }

  3. #3
    The Reel Thing
    Join Date
    Jun 2005
    Posts
    44
    Yes, I tried what you said, and I still get an error when trying to add another struct to the vector.

    Code:
    triangles.push_back(Triangle());
    I got an error because function hasn't been delcared, supposidly.
    Bagpipes – putting the fun back in funeral.

  4. #4
    I am me, who else?
    Join Date
    Oct 2002
    Posts
    250
    Quote Originally Posted by Highland Laddie
    Yes, I tried what you said, and I still get an error when trying to add another struct to the vector.

    Code:
    triangles.push_back(Triangle());
    I got an error because function hasn't been delcared, supposidly.

    Well what is Triangle? (Case does matter!) If you say


    Code:
    TRIANGLE trianglestruct;
    //add some initialization here
    triangles.push_back(trianglestruct);
    That should work fine. It looks to me like you might be a bit confused... it looks like you are trying to call a function or perhaps make a new triangle(??) of type TRIANGLE. You can't really define a function in a struct, and also, if you are trying to make a "new" TRIANGLE, you would want to add another variable, or use the 'new' operator.
    Last edited by dpro; 09-13-2005 at 06:06 PM. Reason: bad case on my part :/

  5. #5
    The Reel Thing
    Join Date
    Jun 2005
    Posts
    44
    Uhh, yeah, it should. I found the problem. It was me. I made a stupid mistake, I already forget what it was, but it was stupid. It's working now, by the way. :P

    Thanks a bunch!
    Bagpipes – putting the fun back in funeral.

  6. #6
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    This compiles VC++ 6.0 (not an authority for C++ standards by any means!) but also on the latest version of Dev-C++ 4.9.9 (which uses g++, somewhat more trustworthy?). I am not all that sure if this truely makes it valid, but it seems logical. I call the class constructor to return a new class object to throw in the vector. If instantiating the object beforehand is necessary to do this, then whatev, but it does seem at least logical if not syntactically correct.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Unless you need part of your code to work in C, you can just change
    Code:
    typedef struct Triangle
    {
    // ...
    } TRIANGLE;
    to
    Code:
    struct Triangle
    {
    }
    and use Triangle everywhere to avoid confusion between Triangle and TRIANGLE. The typdef is only helpful in C, not C++.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. Structures, passing array of structures to function
    By saahmed in forum C Programming
    Replies: 10
    Last Post: 04-05-2006, 11:06 PM
  3. Input output with structures
    By barim in forum C Programming
    Replies: 10
    Last Post: 04-27-2004, 08:00 PM
  4. pointers to arrays of structures
    By terryrmcgowan in forum C Programming
    Replies: 1
    Last Post: 06-25-2003, 09:04 AM
  5. Methods for Sorting Structures by Element...
    By Sebastiani in forum C Programming
    Replies: 9
    Last Post: 09-14-2001, 12:59 PM