Sign in to follow this  

How do I pass a class array into a function?

This topic is 4838 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

I have an array of classes. My class is a Rectangle class. I want to pass my array into a Collision Detection function but I don't know how. It doesn't work just like passing an array of integers. Here is my prototype I've tried BOOL CanMove(DIRECTION Dir, c2DRect *Rect); BOOL CanMove(DIRECTION Dir, c2DRect *Rect[]; BOOL CanMove(DIRECTION Dir, c2DRect Rect[]); They all seem not to work. Any ideas would be appreciated.

Share this post


Link to post
Share on other sites
Quote:
Original post by andyb716
I have an array of classes.


can we see how you've declared it?



c2DRect* rect;

declares a variable of type pointer to c2DRect, the address could refer to one instance or the first element of an array of c2DRect.


c2DRect* Rect[];

declares an array of pointers to c2DRect, each element can have an address that refers to one instance of c2DRect or the first element of an array of c2DRect.


c2DRect Rect[]

declares an array of c2DRect.

Quote:
Original post by andyb716
They all seem not to work.


What exactly happens? if you get an error whats the error message.

Quote:
Original post by andyb716
Any ideas would be appreciated.


[smile]


#include <vector>

typedef std::vector<c2DRect> vec_of_c2DRects;

vec_of_c2DRects my_rects;

/* ....... */

bool CanMove(DIRECTION Dir, const vec_of_c2DRects& stdRect);

Share this post


Link to post
Share on other sites
Declaration
c2DRect m_Rect2[8];

These are my errors.

BOOL CanMove(DIRECTION Dir, c2DRect *Rect[])
cannot convert parameter 2 from 'class c2DRect (*)[8]' to 'class c2DRect *[]'

BOOL CanMove(DIRECTION Dir, c2DRect *Rect)
cannot convert parameter 2 from 'class c2DRect (*)[8]' to 'class c2DRect *'

BOOL CanMove(DIRECTION Dir, c2DRect Rect[])
annot convert parameter 2 from 'class c2DRect (*)[8]' to 'class c2DRect []'

thanks snk_kid for the help I'll try the vector.

Share this post


Link to post
Share on other sites
You're probably calling the function like CanMove(dir, &m_Rect); It is unnecessary to take the address of an "array" to pass it to a function via a pointer. The explanation is long and will tear down a lot of what you think you know about C++, so just use std::vector until you're ready for it.

Share this post


Link to post
Share on other sites
I would be easier if you showed the line that was generating the error. But in any case, here is what the call should look like for each declaration.

c2DRect m_Rect2[8];

BOOL CanMove(DIRECTION Dir, c2DRect *Rect);

CanMove( dir, m_Rect2 );


BOOL CanMove(DIRECTION Dir, c2DRect *Rect[]);

You have to declare m_Rect2 diffently to use this because
the parameter is an array of pointers.


BOOL CanMove(DIRECTION Dir, c2DRect Rect[]);

CanMove( dir, m_Rect2 );

Share this post


Link to post
Share on other sites
Quote:
Original post by Oluseyi
It is unnecessary to take the address of an "array" to pass it to a function via a pointer. The explanation is long...


You could just give him the short answer: array types are (essentially) pointers!

Quote:
Original post by Oluseyi
and will tear down a lot of what you think you know about C++, so just use std::vector until you're ready for it.


lol... if andyb716 is having trouble with pointer and array notation, I don't think he's going to find understanding iterators any easier (sure, using them might be easier if they're taken with blind faith)! Furthermore, using STL containers with user types can be very inefficient... particularly with std:vector... and a collision detection module is certainly one area where you want efficiency.

Just my $0.02 worth...

Cheers,

Timkin

Share this post


Link to post
Share on other sites

This topic is 4838 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this