Skip to main content
GameDev.net gamedev.net
🔒 Locked

STL Vector problem

Started by stodge Mar 21, 2003 at 3:45 PM 11 replies 300+ views
Original Post
stodge
stodge
I'm trying to do the following:
quote:
std::vector<float[3]> HBvertexArray;
I'm getting an error:
quote:
g:\sdk\stlport\stlport\stl\_construct.h(85) : error C2538: new : cannot specify initializer for arrays
Does this mean I can't do the above? Thanks [edited by - stodge on March 21, 2003 4:45:40 PM] [edit: fixed <'s & >'s, use the html & g t ; and & l t ; to display angle-brackets (without spaces)] [edited by - Magmai Kai Holmlor on March 21, 2003 9:25:54 PM]
---------------------http://www.stodge.net
Feydrex
Feydrex
Are you giving the vector a type, since it is a template? Something like:

  std::vector<int> iVec;  

stodge
stodge
Argh! I used the quote tags instead of source. The forum script wiped out some of the code:


   typedef std::vector<float[3]> HBvertexArray;  
---------------------http://www.stodge.net
civguy
civguy
Seems like you can't do it then. But maybe you could do this:

    
template <class T, int size>
class Array {
public:
T& operator[](int i) {
return array[i];
}

const T& operator[](int i) const {
return array[i];
}
private:
T array[size];
};

std::vector<Array<float, 3> > HBvertexArray;

//and access it normally, like:


HBvertexArray[3][0] = 2;


[edited by - civguy on March 21, 2003 5:20:10 PM]
stodge
stodge
Thanks civguy. I think I''ll try doing this:


  		typedef struct
{
HBfloat x;
HBfloat y;
HBfloat z;
} HBvertex;

typedef std::vector<HBvertex> HBvertexArray;
---------------------http://www.stodge.net
Shannon Barber
Shannon Barber
That will work. You can use this sneaky code so that you can use either .x/y/z or [1,2,3] to access the vertex coordinates:

struct point3D
{
float x,y,z;
float& operator[](size_t index)
{
//do bounds check in debug build,
// size_t is unsigned, so index can never be negative
assert(index < 3);
return (&x)[index];
}
};

point3D v;
v.y = 1.00;
v[1] == 1.00;


Er, wait. That's not a vertex yet, it's just a point until you associate some other information with the point (e.g. color, normal, texture mapping coor.).

[edited by - Magmai Kai Holmlor on March 21, 2003 9:36:25 PM]
The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Beer Hunter
Beer Hunter
quote:
Original post by Magmai Kai Holmlor
...this sneaky code...
Why "sneaky"? Point3D is a POD type, and is layout-compatible with an array of three floats. It''s valid code.
stodge
stodge
Thanks Magmai; I''ll try that code snippet out.
---------------------http://www.stodge.net
Shannon Barber
Shannon Barber
It''s not obvious to a beginner, and as you indicate there are issues in the dark if the structure definition changes.
The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
civguy
civguy
quote:
Original post by Beer Hunter
Why "sneaky"? Point3D is a POD type, and is layout-compatible with an array of three floats. It's valid code.
Is it really? Is there a quarantee that there can't be padding between the members? Even if it worked in all compilers, I wouldn't call it valid code. More like kludgy but working code.

[edited by - civguy on March 22, 2003 3:06:30 AM]
YoshiN
YoshiN
quote:
Original post by Magmai Kai Holmlor
That will work. You can use this sneaky code so that you can use either .x/y/z or [1,2,3] to access the vertex coordinates:

struct point3D
{
float x,y,z;
float& operator[](size_t index)
{
//do bounds check in debug build,
// size_t is unsigned, so index can never be negative
assert(index < 3);
return (&x)[index];
}
};

point3D v;
v.y = 1.00;
v[1] == 1.00;


Er, wait. That''s not a vertex yet, it''s just a point until you associate some other information with the point (e.g. color, normal, texture mapping coor.).

[edited by - Magmai Kai Holmlor on March 21, 2003 9:36:25 PM]


Are you sure that x, y and z will always be stored linearly and in that order?
-YoshiXGXCX ''99

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.