School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!
Welcome to Dream.In.Code
Become an Expert!

Join 340,115 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 4,306 people online right now. Registration is fast and FREE... Join Now!



Nested Structures and Constructors

Nested Structures and Constructors Need help with a constructor for nested structures Rate Topic: -----

#1 not1975  Icon User is offline

  • New D.I.C Head
  • Pip
  • Group: Members
  • Posts: 46
  • Joined: 26-December 06


Dream Kudos: 0

Posted 18 February 2007 - 04: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.


struct Point
{
	int x, y;
	Point::Point(int,int);
};

Point::Point(int xpos, int ypos)
{
	x = xpos;
	y = ypos;
}

struct Velocity
{
	int x, y;
	Velocity::Velocity(int,int);
};

Velocity::Velocity(int xvel, int yvel)
{
	x = xvel;
	y = yvel;
}

struct Circle
{
	Point position;
	Velocity velocity;
	int radius;
	int color;
	void Circle::coll_detect();
	void Circle::move_circle();
	Circle::Circle(Point& pos, Velocity& vel, int rad, int col);
	void Circle::draw(BITMAP*);
};

Circle::Circle(Point& pos, Velocity& vel, int rad, int col)
{
	position.x = pos.x;
	position.y = pos.y;
	velocity.x = vel.x;
	velocity.y = vel.y;
	radius = rad;
	color = col;
}

Circle ball( {15, 15}, {5, -5}, 15, makecol(0,0,255));





This post has been edited by not1975: 18 February 2007 - 04:57 PM

Was This Post Helpful? 0
  • +
  • -


#2 gregoryH  Icon User is offline

  • D.I.C Regular
  • Icon
  • View blog
  • Group: Mentors
  • Posts: 430
  • Joined: 04-October 06


Dream Kudos: 50

Posted 19 February 2007 - 02:12 AM

View Postnot1975, on 18 Feb, 2007 - 05:49 PM, said:

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:
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:
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:

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 February 2007 - 02:33 AM

Was This Post Helpful? 0
  • +
  • -

#3 not1975  Icon User is offline

  • New D.I.C Head
  • Pip
  • Group: Members
  • Posts: 46
  • Joined: 26-December 06


Dream Kudos: 0

Posted 19 February 2007 - 08:10 AM

There seems to be a problem with the "=" operator. I'm getting the same "no matching function for call to 'Point::Point()' / 'Velocity::Velocity()' "

This post has been edited by Dark_Nexus: 19 February 2007 - 12:00 PM

Was This Post Helpful? 0
  • +
  • -

#4 gregoryH  Icon User is offline

  • D.I.C Regular
  • Icon
  • View blog
  • Group: Mentors
  • Posts: 430
  • Joined: 04-October 06


Dream Kudos: 50

Posted 21 February 2007 - 03:34 AM

View Postnot1975, on 19 Feb, 2007 - 09:10 AM, said:

There seems to be a problem with the "=" operator. I'm getting the same "no matching function for call to 'Point::Point()' / 'Velocity::Velocity()' "

Hi

I got that exact same error initially using your code. I have re-tested the operator= and it appears correct.

These empty constructors are called when Circle constructor prepares its working space.... long before the operator = is required. Did you make a Point(){}; and a Velocity(){}; in each struct? Until you have those actually written, you may still have some issues.

There may be an issue with the signature of Circle, as Circle(Point& pos, Velocity& vel, int rad, int col) should be Circle(Point pos, Velocity vel, int rad, int col).

This post has been edited by gregoryH: 21 February 2007 - 03:39 AM

Was This Post Helpful? 0
  • +
  • -

#5 not1975  Icon User is offline

  • New D.I.C Head
  • Pip
  • Group: Members
  • Posts: 46
  • Joined: 26-December 06


Dream Kudos: 0

Posted 21 February 2007 - 02:03 PM

View PostgregoryH, on 21 Feb, 2007 - 04:34 AM, said:

Did you make a Point(){}; and a Velocity(){}; in each struct? Until you have those actually written, you may still have some issues.


I don't know how I actually forgot that. That fixes my constructor problems. Now I just have one problem:


struct Circle
{
	Point position;
	Velocity velocity;
	int radius;
	int color;
	void Circle::coll_detect(int,int);
	void Circle::move();
	Circle::Circle(Point pos, Velocity vel, int rad, int col);
	void draw(BITMAP* bitmap)	 //line 70
	{
		circlefill(bitmap, position.x, position.y, radius, color);
	};
};




It doesn't seem to like my draw function. It gives me these errors:

70 variable or field `draw' declared void
70 expected `;' before '(' token
Was This Post Helpful? 0
  • +
  • -

#6 gregoryH  Icon User is offline

  • D.I.C Regular
  • Icon
  • View blog
  • Group: Mentors
  • Posts: 430
  • Joined: 04-October 06


Dream Kudos: 50

Posted 21 February 2007 - 11:46 PM

View Postnot1975, on 21 Feb, 2007 - 03:03 PM, said:


struct Circle
{
	Point position;
	Velocity velocity;
	int radius;
	int color;
	void Circle::coll_detect(int,int);
	void Circle::move();
	Circle::Circle(Point pos, Velocity vel, int rad, int col);
	void draw(BITMAP* bitmap)	 //line 70
	{
		circlefill(bitmap, position.x, position.y, radius, color);
	};
};




It doesn't seem to like my draw function. It gives me these errors:

70 variable or field `draw' declared void
70 expected `;' before '(' token

Hmm

If I may return to your original post, there are no headers to include the graphics package into your code. I notice that there is a type of BITMAP, which is not defined anywhere, so logically its either in the graphics header or needs to be defined.

Hope this helps

regards


Greg
Was This Post Helpful? 0
  • +
  • -

#7 not1975  Icon User is offline

  • New D.I.C Head
  • Pip
  • Group: Members
  • Posts: 46
  • Joined: 26-December 06


Dream Kudos: 0

Posted 22 February 2007 - 03:44 AM

I included the allegro header file, and moved all the function definitions into the structures, and now it works. It didn't seem to like me defining functions outside of the structures. Anyways, I got it working now. Thanks for all the help.
Was This Post Helpful? 0
  • +
  • -

#8 gregoryH  Icon User is offline

  • D.I.C Regular
  • Icon
  • View blog
  • Group: Mentors
  • Posts: 430
  • Joined: 04-October 06


Dream Kudos: 50

Posted 22 February 2007 - 01:34 PM

View Postnot1975, on 22 Feb, 2007 - 04:44 AM, said:

I included the allegro header file, and moved all the function definitions into the structures, and now it works. It didn't seem to like me defining functions outside of the structures. Anyways, I got it working now. Thanks for all the help.

HI

Glad to help you. That is why I joined this great web site.

In the Thai language.. Mai Ben Lai
Was This Post Helpful? 0
  • +
  • -



Fast Reply

  

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users



Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month