Archived

This topic is now archived and is closed to further replies.

OoMMMoO

easy class help please

Recommended Posts

OoMMMoO    122
I have a global class array like: class world { int x; int y; }; world rooms[10][10]; what I wanted to know is why cant I initialize the values of each object unless I''m in a function? If I do it in a function it will only be changed in that function and no where else right? The way I''ve been doing it is in my main function which is a pain. can someone help me out? If I do it outside of main I get a ton of errors. pppppllllleeeeaaassssseee help!!!

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
What you need is a class constructor. Redefine you class as:

class world
{
public:
int x, y;

world( void ) { x = y = 0; }
world(int tx, int ty) { x = tx; y = ty; }
};

world rooms[][10] = {
{ world(0, 0), world(0, 1), ... world(0, 9) },
{ world(1, 0), world(1, 1), ... world(1, 9) },
...
{ world(9, 0), world(9, 1), ... world(9, 9) }
};

Also, don''t forget the public: bit otherwise you won''t be able to access the x and y variables.

Regards
Michael

Share this post


Link to post
Share on other sites