Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,767 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,591 people online right now. Registration is fast and FREE... Join Now!




Nested Structures and Constructors

 
Reply to this topicStart new topic

Nested Structures and Constructors, Need help with a constructor for nested structures

not1975
18 Feb, 2007 - 04:49 PM
Post #1

New D.I.C Head
*

Joined: 26 Dec, 2006
Posts: 43


My Contributions
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.

CODE


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 Feb, 2007 - 04:57 PM
User is offlineProfile CardPM
+Quote Post

gregoryH
RE: Nested Structures And Constructors
19 Feb, 2007 - 02:12 AM
Post #2

D.I.C Regular
Group Icon

Joined: 4 Oct, 2006
Posts: 417


Dream Kudos: 50
My Contributions
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
User is offlineProfile CardPM
+Quote Post

not1975
RE: Nested Structures And Constructors
19 Feb, 2007 - 08:10 AM
Post #3

New D.I.C Head
*

Joined: 26 Dec, 2006
Posts: 43


My Contributions
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 Feb, 2007 - 12:00 PM
User is offlineProfile CardPM
+Quote Post

gregoryH
RE: Nested Structures And Constructors
21 Feb, 2007 - 03:34 AM
Post #4

D.I.C Regular
Group Icon

Joined: 4 Oct, 2006
Posts: 417


Dream Kudos: 50
My Contributions
QUOTE(not1975 @ 19 Feb, 2007 - 09: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()' "

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 Feb, 2007 - 03:39 AM
User is offlineProfile CardPM
+Quote Post

not1975
RE: Nested Structures And Constructors
21 Feb, 2007 - 02:03 PM
Post #5

New D.I.C Head
*

Joined: 26 Dec, 2006
Posts: 43


My Contributions
QUOTE(gregoryH @ 21 Feb, 2007 - 04:34 AM) *

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:

CODE


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
User is offlineProfile CardPM
+Quote Post

gregoryH
RE: Nested Structures And Constructors
21 Feb, 2007 - 11:46 PM
Post #6

D.I.C Regular
Group Icon

Joined: 4 Oct, 2006
Posts: 417


Dream Kudos: 50
My Contributions
QUOTE(not1975 @ 21 Feb, 2007 - 03:03 PM) *

CODE


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

User is offlineProfile CardPM
+Quote Post

not1975
RE: Nested Structures And Constructors
22 Feb, 2007 - 03:44 AM
Post #7

New D.I.C Head
*

Joined: 26 Dec, 2006
Posts: 43


My Contributions
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.
User is offlineProfile CardPM
+Quote Post

gregoryH
RE: Nested Structures And Constructors
22 Feb, 2007 - 01:34 PM
Post #8

D.I.C Regular
Group Icon

Joined: 4 Oct, 2006
Posts: 417


Dream Kudos: 50
My Contributions
QUOTE(not1975 @ 22 Feb, 2007 - 04: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.

HI

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

In the Thai language.. Mai Ben Lai
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/8/09 06:25AM

Be Social

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

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month