QUOTE(not1975 @ 18 Feb, 2007 - 05:49 PM)

Hey. I'm trying to make a simple graphics program. Although I am using Allegro, I don't need help with the Allegro code, or the graphics, et cetera, so I think this is appropriate.
The problem is that the structure Circle has other structures nested in it: Point and Velocity. This worked fine, until I tried making a constructor; I don't how to make a constructor for structures that have other structures nested within them.
The constructor doesn't work. It probably isn't even close, but I didn't really have a clue what i was doing, and I exhausted all the most obvious choices.
When trying to do things like this, its is important to consider overlaoded operators. I tried your code in Dev C++ and got a number of errors relating to "default" constructors (which take no parameters). As a result, Point was modified to look like this:
CODE
struct Point
{
int x, y;
Point(){};
Point(int,int);
};
The same was applied to Velocity and Circle. While using
Point::Point(.. is allowed, its not the usual practice or required inside the body of a struct or class.
The other thing that you should always create are the assignment (operator=) and copy constructors, you will save much time coding by using them. This now makes Point look like this:
CODE
struct Point
{
int x, y;
Point(){};
Point(int,int);
Point ( const Point &);
Point & operator= ( const Point & );
};
Point::Point(int xpos, int ypos)
{
x = xpos;
y = ypos;
}
Point::Point ( const Point & p)
{
x = p.x;
y = p.y;
}
Point & Point::operator= ( const Point & p)
{
if ( this != p) // prevent self assignment
{
x = p.x;
y = p.y;
}
return * this;
}
Now you have the extra constructors.. lets look at the effect on circles parameterized constructor:
CODE
Circle::Circle(Point& pos, Velocity& vel, int rad, int col)
{
position = pos; // this is the way this changes... much more readable and less code...
velocity.x = vel.x; // now see if you can get the same into velocity.
velocity.y = vel.y;
radius = rad;
color = col;
}
This post has been edited by gregoryH: 19 Feb, 2007 - 02:33 AM