MSVC and Structs

Started by
2 comments, last by AndersG 24 years, 6 months ago
Hey guys! I hope someone can help me out with this problem: I''m making a 3D-engine and I''ve made Structs for vectors, planes, objects etc., like this: struct Vector { float x,y,z; inline void init (float x , float y, float z); inline void operator + (Vector &v2); inline void operator - (Vector &v2); inline void operator * (float scale); }; struct Plane { int p1,p2,p3; Vector Normal; inline void init (int pp1,int pp2,int pp3, float a, float b, float c); }; struct Object { Vector *Vertex; Plane *Face; int Vertices, faces; }; In my main program, i create objects like this: Object *obj[3]; //Three objects obj[0]->Vertices=3; obj[0]->Faces=1; ^ / This is where the problem occurs...As soon as I''m trying to assign a value to a member of a struct, I get a "general protection fault" !! What am I doing wrong? The same code works in BC 5.02...
Advertisement
It doesn''t look like you allocated memory for your structs. You just put the pointers on the stack.
SiCrane's right. You need to do:
for (i=0; i<3; i++)
obj = new Object;<br><br>I have another concern: your method of overloading operators for vector leaves something to be desired. Usually, if you want binary (i.e. taking two arguments) operators to work like standard C types, you should use the following declaration:<br><br>Vector operator + (const Vector &v2);<br><br>const is your friend. Learn it, live it, love it.<br><br>This method lets you chain calls to +:<br>Vector a, b, c;<br><br>Vector tot = a + b + c;<br><br>You should also overload operator +=:<br>Vector& operator += (const Vector &v2)<br>{<br> x += v2.x;<br> y += v2.y;<br> z += v2.z;<br> return this;<br>}<br><br>I highly suggest Meyer's "Effective C++" for a really good discussion on good operator overloading techniques.<br><br>Edited by - Stoffel on 2/18/00 11:16:26 AM
Thank''s for all the helped, it''s working all better
now!!

This topic is closed to new replies.

Advertisement