Archived

This topic is now archived and is closed to further replies.

Several problems with a short C++ program

This topic is 6364 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok, I wrote this up last night (after a long hiatus from C++, thanks to heavy courses ), and it has various errors which I can''t resolve. First error: test.cpp:23: uninitialized reference member ''poly::coord'' Others MAY include runtime crashes... So, what this code is supposed to perform: Store a set of points, which can be shared among polygons (triangles), and modified individually, seperately from the polygons themselves. (modifiers(coordinants))-->values in several polygons. Any help would be much apreciated. Thanks, -Medgur
    
#include <iostream>

class p3d
{
 public:
 float x; float y; float z;
 p3d operator = (p3d &point);
 p3d() {x = 0; y  = 0; z = 0;}
 p3d(float val1, float val2, float val3) {x = val1; y = val2; z = val3;}
};

p3d p3d::operator = (p3d &point)
{
 x = point.x; y = point.y; z = point.z;
 return *this;
}

class poly
{
 public:
 p3d (&coord)[2];
 int red; int green; int blue;
 poly() {red = 0; green = 0; blue = 0;}
};

int main(int argc,int *argv[])
{
 poly tp;
 p3d vals[2];
 vals[0].x = 1; vals[0].y = 1; vals[0].z = 1;
 vals[1].x = 2; vals[1].y = 2; vals[1].z = 2;
 vals[2].x = 2; vals[2].y = 2; vals[2].z = 2;
 tp.coord = vals;
 tp.red = 0; tp.blue = 0; tp.green = 0;
 cout << "Initial x value: " << tp.coord[0].x << endl;
 vals[0].x = 5;
 cout << "Final x value: " << tp.coord[0].x << endl;
 return 0;
}
    

Share this post


Link to post
Share on other sites
quote:
Original post by Medgur
test.cpp:23: uninitialized reference member 'poly::coord'



Your problem is with the references in class poly. References MUST be initialized at creation; specifically, as members of the class poly, they must be initialized in poly's constructor initializer lists.

Given that, you might recreate your code like this:

            
class poly {
public:
p3d &a, &b, &c;
int red;
int green;
int blue;
poly(const p3d& ra, const p3d& rb, const p3d& rc)
: a(ra), b(rb), c(rc)
{red = 0; green = 0; blue = 0;}
};

int main(int argc,int *argv[])
{
p3d vals[3];
vals[0].x = 1; vals[0].y = 1; vals[0].z = 1;
vals[1].x = 2; vals[1].y = 2; vals[1].z = 2;
vals[2].x = 2; vals[2].y = 2; vals[2].z = 2;

poly tp(vals[0], vals[1], vals[2]);
tp.red = 0; tp.blue = 0; tp.green = 0;

cout << "Initial x value: " << tp.coord[0].x << endl;
vals[0].x = 5;
cout << "Final x value: " << tp.coord[0].x << endl;

return 0;
}


If you really want to use references (as I suspect, viewing your testing of this property at the end of main), something like this should work. However, you could get similar results easier using pointers and an integer (constant or variable) representing the number of items.

Also note that I changed your array size to 3 from 2, since arrays of size 2 range only from 0 to 1, not 0 to 2.

---- --- -- -
Blue programmer needs food badly. Blue programmer is about to die!


Edited by - mossmoss on July 14, 2000 1:24:40 PM

Share this post


Link to post
Share on other sites
Thanks for the rapid response! Very helpful indeed... One last problem though:
Is it at all possible to maintain the points of type p3d in the poly class as an array? (I would like to easily change the amount of points, without too much additional code in, say, rasterization).

Thanks,
-Medugr

Edited by - Medgur on July 14, 2000 2:31:10 PM

Share this post


Link to post
Share on other sites
quote:
Original post by Medgur
Is it at all possible to maintain the points of type p3d in the poly class as an array? (I would like to easily change the amount of points, without too much additional code in, say, rasterization).



It is certainly possible to keep them as an array of OBJECTS...

    
class poly {
p3d coord[3];
};


HOWEVER, I don''t think you can have an array of references. The references have to be initialized at creation, and I don''t think it''s possible to initialize an array of references all at once. (I haven''t tested this nor have I examined my C++ books, so I suppose it''s possible, but I don''t think so.)



---- --- -- -
Blue programmer needs food badly. Blue programmer is about to die!

Share this post


Link to post
Share on other sites