Results 1 to 10 of 10

Thread: iterator undeclared error?

  1. #1

    Thread Starter
    Addicted Member ChuckB's Avatar
    Join Date
    Jul 2002
    Location
    South Carolina, USA
    Posts
    157

    iterator undeclared error?

    Hi,

    I can create a vector. I need an iterator so I can index the vector I have created. However, I get an error "Iterator undeclared" when I do this. What is missing?

    Code:
    #include <vector>
    #include <iterator>
    
    . . .
    
    header.num_vertices=200;
    std::vector<VERTEX> v;
    v.resize(header.num_vertices);
    std::iterator idx;
    idx=v.begin();
    VERTEX is a structure of x,y and z floats.

    Regards,
    ChuckB

  2. #2
    Ya ya Baby!!!Me is Back
    Join Date
    Jul 2002
    Posts
    362
    you have using namespace std;

    or using std::vector; using std:iterator;

  3. #3
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710

    Re: iterator undeclared error?

    Originally posted by ChuckB

    Code:
    ...
    std::vector<VERTEX> v;
    v.resize(header.num_vertices);
    std::vector<VERTEX>::iterator idx;
    idx=v.begin();
    The iterator is an inner class of the container. Create them like the above. In many cases, you can typedef them, as well:
    Code:
    class myClass
    {
    public:
      std::vector<int> x;
      typedef std::vector<int>::iterator vec_it;
    };
    And just use vec_it as the type.

    Z.

  4. #4

    Thread Starter
    Addicted Member ChuckB's Avatar
    Join Date
    Jul 2002
    Location
    South Carolina, USA
    Posts
    157
    Hi,
    Thanks for the correct format. I had actually just figured it out before logging in. ;-)

    I have my first binary read/write functions that utilize vectors. I can save the number of textures, vertices and quads to a file and retrieve the data...all in binary. I did create a header to help me track it all. With the help of sizeof() I can get just about anything done.

    One question though. I have a 'vertex' vector v[100], for example. I tried writing the entire vector to the file at one time...but it didn't like that. I had to write one vector element at a time. Is there a way to grab the whole vector.

    Code:
    for (int x=1;x<header.num_vertices +1;x++)
       fwrite(&v[x],sizeof(v[x]),1,fp);
    Also, I couldn't get the iterator to work as I wanted...so I went back to using 'x' above. I was hoping to iterate through the vectors with iter++.

    Regards,
    ChuckB

  5. #5
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    You could PROBABLY write the vector out like this by taking the address of the first item in the vector:
    Code:
    &v[0]
    Like I said before, you have to be careful with this, because the standard does NOT specify how a container is represented internally, so you dont actually know if the internal buffer is a raw pointer or not.

    Z.

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Since pointers can be used as iterators, you could maybe load a bunch of vectors into a temporary mem block and then use vector::insert to insert them like this:

    Code:
    VERTEX buffer[10];
    // fill the buffer
    vec.insert(buffer, buffer+10);
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  7. #7

    Thread Starter
    Addicted Member ChuckB's Avatar
    Join Date
    Jul 2002
    Location
    South Carolina, USA
    Posts
    157
    Hi,
    My VB World Map maker is working fine. I can create tons of vertices for QUADS for my 3D world.

    I am reading an ASCII text file (for now) and loading the values into a vector. This is working provided I tell the function how many lines to read. Here is the short section of code.

    Code:
      VERTEX v;
      char oneLine[255];
      FILE *fp;
      fp=fopen("mydata.dat","rt");
      q.reserve(96);                            //< --- hardcoded
      q.begin();
      for (int i=0;i<95;i++){                //< --- hardcoded
          ReadString(fp,oneLine);
          sscanf(oneLine,"%f, %f, %f", &v.x,&v.y,&v.z);
          q[i]=v;
      }
      fclose(fp);
    Now, I know how to get VB to save the number of vertices at the top of the file as Z has suggested before. However, I would like to make this code go dynamically without knowing this number in advance.

    So, any clues on how to increment to read all vertices from the file? I think I need to change the 'for' line to read something like:

    for (vector::iterator iter = v.begin();iter < v.end();iter++)...etc.

    After I get the vectors figures out completely I will be adding a header to my vertex files and saving in binary...so this exercise is academic for me...I want to know how to do it. :-)

    It has been said before that arrays are faster than vectors. Are we talking about a lot of time difference regarding that?

    Regards,
    ChuckB

  8. #8
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    No, not really. Since loading is done only once and the really slow thing is the disk operation the overhead from possible reallocations of the vector is neglectible. Reading from a vector is basically just a range test and one (possibly inlined) function call of overhead, so that's not too bad.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  9. #9

    Thread Starter
    Addicted Member ChuckB's Avatar
    Join Date
    Jul 2002
    Location
    South Carolina, USA
    Posts
    157
    CornedBee,
    Cool! Vectors sound cooler than arrays. :-) I'll use them.

    Any idea on how to dynamically increase the size when required like REDIM PRESERVE array(x) in VB?


    Regards,
    ChuckB

  10. #10
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    It'll do it automatically. However, you can give it a hint if you know vaguely what you're going to put into it. Use .reserve()
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width