More problems with a short C++ program

Started by
3 comments, last by Medgur 23 years, 9 months ago
First off, thanks to mossmoss for the early help. But, I've been trying to play around with this, trying to get this small modification to work. In the poly class I'd like to keep the coord member an array, and I can't seem to make it work with the current state of this code. So, without further ado, the simple C++ program. I can't believe I'm having so much trouble with something so simple! 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)[3];
 int red; int green; int blue;
 // Here's the problem line:

 poly(const p3d& ra, const p3d& rb, const p3d& rc)
  : coord(ra)//[0], coord(rb)[1], coord(rc)[2]
  {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;
}
        
Edited by - Medgur on 7/14/00 6:43:15 PM
Advertisement
Medgur,
What type exactly do you want "coord" to be? Do you want an array of 3 references to p3d object, or a reference to an array of 3 p3d objects?

The way it''s delared now (p3d (&coord)[3]) is a reference to an array of 3 elements, but the code in the constructor seems to try to use it as an array of references.
Unfortunately, an array of references is illegal, so it''s not really possible to do what you''re trying to do in the constructor.

However, it is possible to have a reference to an array of 3 elements. This is not as flexable, but it will work with the main function that you currently have.

If you redefine your poly class to be:
class poly{ public:    p3d (&coord)[3];    int red, green, blue;    poly(p3d (&rg)[3]) : coord(rg) { red = green = blue = 0; }}; 

And then instantiate tp using "poly tp(vals);" your program will work.
This is probably not exactly what you were getting at, since each poly object now needs an array of 3 p3d objects instead of any 3 p3d objects, but I think that''s the best you can do with references.
If you really want to use any three p3d objects, you''ll have to either use an array of pointers or 3 individual references in your poly class.

Hope this helps.
...Syzygy
Thanks for the help!
No wonder I had so much trouble trying to get it to work...

Thanks,
-Medgur

This seems to work:
        #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)[3]; int red; int green; int blue; poly(p3d (*points)[3])  : coord(*points)  {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); 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;}        



Edited by - Medgur on July 17, 2000 3:15:13 PM
small style note:
usually, when you overload the assignment operator, you want its argument to be a const reference. The way it is now, you wouldn''t be able to assign a p3d to a const p3d.

Also, it''s usually standard for the assignment operator to return a reference to *this, so you can chain assignments, but it''s not necessary.

So, with those changes, the assignment operator would look like this:
    p3d& p3d::operator = (const p3d& point){  x = point.x;  y = point.y;  z = point.z;  return *this;}// which allows code like this:void copy_point (const p3d& point){  p3d a = point; // not legal in your current code}// or this:  p3d a, b, c;  a = b = c = point;    


(See "Effective C++", Meyers, for good tips on overloading classes so that they act like C types)
Oops... Missed the const. Thanks for the heads up.

-Medgur

Edited by - Medgur on July 17, 2000 3:16:29 PM

This topic is closed to new replies.

Advertisement