Results 1 to 13 of 13

Thread: vector

  1. #1
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default vector

    HI, I need a vector such that every elemet is an array of 4 elements; I try this....but I don't sure it works....but then must use in setLight
    Qt Code:
    1. std::vector <GLfloat>position[4];
    2. position->push_back(1);
    3. position[0][0] = 0.0f;
    4. position[0][1] = 1.0f;
    5. position[0][2] = 2.0f;
    6. position[0][3] = 0.0f;
    7. glLightfv(GL_LIGHT0, GL_POSITION, position[0]); //this don't compile
    To copy to clipboard, switch view to plain text mode 
    Last edited by mickey; 26th April 2006 at 09:23.
    Regards

  2. #2
    Join Date
    Jan 2006
    Posts
    73
    Thanks
    16
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: vector

    vould
    Qt Code:
    1. std::vector<std::vector<int> > cont;
    2. std::vector<int> temp;
    3. temp.push_back(1);
    4. temp.push_back(2);
    5. temp.push_back(3);
    6. temp.push_back(4);
    7. cont.push_back(temp);
    8. int k;
    9. k = cont[0][0];
    To copy to clipboard, switch view to plain text mode 
    do the trick?

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: vector

    Quote Originally Posted by mickey
    glLightfv(GL_LIGHT0, GL_POSITION, position[0]); //this don't compile
    It's because OpenGL doesn't know anything about STL containers --- it's a C library.

    Since std::vector keeps all elements in a single block, you can try:
    Qt Code:
    1. glLightfv( GL_LIGHT0, GL_POSITION, &(position[0][0]) );
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: vector

    It's compile! But I see that sometimes the instructions below get 'debug error'...What's the problem?
    Qt Code:
    1. std::vector <GLfloat>position[4];
    2. position->push_back(1);
    3. position[0][0] = 0.0f;
    4. position[0][1] = 1.0f;
    5. position[0][2] = 2.0f;
    6. position[0][3] = 0.0f;
    To copy to clipboard, switch view to plain text mode 
    Regards

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: vector

    Quote Originally Posted by mickey
    std::vector <GLfloat>position[4];
    I hope you realize that this is an array of 4 vectors, not vector of 4-element arrays.

  6. #6
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: vector

    I suspected this; but in first post you 'didn't correct me'... and I was believing that!
    Re-post first question; I need a <vector> such that every element is an array[4];
    ---
    In realty, I have declare vector<GLfloat> pos[4] inside a class declaration! So I think it's OK(?). I can't create instance in class declaration(???).
    Last edited by mickey; 26th April 2006 at 22:15.
    Regards

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: vector

    Quote Originally Posted by mickey
    I need a <vector> such that every element is an array[4];
    Then try this:
    Qt Code:
    1. std::vector< GLfloat[4] > position;
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: vector

    hi, this below compile but I've problem to insert element:
    Qt Code:
    1. std::vector <GLfloat[4]> pos;
    2. std::vector <GLfloat[4]> pos(1); //don't compile
    3. pos.push_back(?); how to insert element?
    To copy to clipboard, switch view to plain text mode 
    Regards

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: vector

    Quote Originally Posted by mickey
    hi, this below compile but I've problem to insert element:
    Qt Code:
    1. std::vector< GLfloat[4] > pos;
    2. GLfloat p[4];
    3. p[0] = 0.0f;
    4. p[1] = 1.0f;
    5. p[2] = 2.0f;
    6. p[3] = 3.0f;
    7. pos.push_back( p );
    To copy to clipboard, switch view to plain text mode 

  10. #10
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: vector

    Hi, I've alreadt tried this but compiler says me
    Qt Code:
    1. C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\vector(810): error C2440: 'initializing' : cannot convert from 'const GLfloat [4]' to 'float [4]'
    To copy to clipboard, switch view to plain text mode 

    If I change to this:
    Qt Code:
    1. std::vector <float[4]> position;
    2. float pos[4];
    3. pos[0] = 0.0;
    4. pos[1] = 1.0;
    5. pos[2] = 0.0;
    6. pos[3] = 0.0;
    7. position.push_back(pos);
    To copy to clipboard, switch view to plain text mode 
    compiler get me the same error.....?!
    Last edited by mickey; 27th April 2006 at 22:45.
    Regards

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: vector

    Then use the const_cast to get rid of "const" or create your arrays on heap:

    Qt Code:
    1. std::vector <GLfloat*> position;
    2. GLfloat *pos new GLfloat[4];
    3. pos[0] = 0.0;
    4. pos[1] = 1.0;
    5. pos[2] = 0.0;
    6. pos[3] = 0.0;
    7. position.push_back(pos);
    To copy to clipboard, switch view to plain text mode 

    And remember to delete[] the array when you no longer need it to avoid memory leaks.

    Or better yet, make your own class, something like:

    Qt Code:
    1. struct MyData {
    2. MyData(GLfloat d1, GLfloat d2, GLfloat d3, GLfloat d4){ d[0] = d1; d[1]=d2; d[2]=d3; d[3]=d4;}
    3. GLfloat d[4];
    4. GLfloat & operator[](int i){ return d[i]; }
    5. }
    6.  
    7. std::vector<MyData> pos;
    8. pos.push_back(MyData(0.0, 1.0, 2.0, 3.0));
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 27th April 2006 at 22:55.

  12. #12
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: vector

    thanks, the first example compile, but I'm trying to understand better the thing trying const_cast....but don't compile this...
    Qt Code:
    1. GLfloat* p = const_cast <GLfloat[4]> (pos);
    2. //const_cast <GLfloat[4]> (pos); //has it sense?
    3. position.push_back(p); //this don't compile
    To copy to clipboard, switch view to plain text mode 
    Regards

  13. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: vector

    const_cast may not work here or may cause segfaults later on, as you create those arrays on stack and when you push them onto the vector, you'll only push the pointer to the array, which will become invalid when the array goes out of scope. So you should stick with creating those items on heap like in my first example.

  14. The following user says thank you to wysota for this useful post:

    ydz185 (4th May 2006)

Similar Threads

  1. Replies: 4
    Last Post: 29th December 2008, 19:50
  2. vector of vector and computes
    By mickey in forum General Programming
    Replies: 1
    Last Post: 15th May 2008, 12:47
  3. Vector allocation
    By ToddAtWSU in forum General Programming
    Replies: 4
    Last Post: 22nd June 2007, 21:37
  4. insert in a vector of vector
    By mickey in forum General Programming
    Replies: 3
    Last Post: 6th March 2007, 08:45
  5. vector of vector size
    By mickey in forum General Programming
    Replies: 5
    Last Post: 13th February 2007, 15:59

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.