a nooby class question ;)

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
chrizZz
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 14
Joined: Tue Jan 27, 2009 5:12 am

a nooby class question ;)

Post by chrizZz »

hello everybody this is my first question in the Programming Discussion !
i hope this is seen as an valid nobby question here in this forum ^^

i have created 2 classes and 1 function.
my function checks one variable from class 1 and one variable from class 2 : function("variable_from_class1", "variable_from_class2") the function returns a boolian value (but that doesn't matter).

my problem is that i have different functionmembers in class 1 and one of those members checks my function (with the "variable_from_class1" and "variable_from_class2") so i get an error that i have
to declare "the variable_from_class2".

what can i do ?

thank you guys :)


edit: ok i used a global variable this time but isn't there any other chance to import a class member to another ? maybe with class friendship ? ^^
User avatar
eatcomics
ES Beta Backer
ES Beta Backer
Posts: 2528
Joined: Sat Mar 08, 2008 7:52 pm
Location: Illinois

Re: a nooby class question ;)

Post by eatcomics »

I'm not sure I understand, you should post some code. Include the classes and the where you are calling the function.
Image
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: a nooby class question ;)

Post by Falco Girgis »

Yeah, post your code. There's a clear misunderstanding about how passing parameters to functions work.
chrizZz
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 14
Joined: Tue Jan 27, 2009 5:12 am

Re: a nooby class question ;)

Post by chrizZz »

uhhh...now this is gonna be awkward ^^
i was studieng a bit of lazy foos sdl tutorial (although i'm aware that i don't feel 100% comfortable with c++..especially classes & pointers)
anyway here is the modified code:

it's a bit much code but these are the two classes "Char" and "Collision" and the function check_collision


SDL_Rect colli; <-- this is how i solved it i use a global variable to hold the information from the class "Collision"

class Char
{
private:
SDL_Rect box;
int xvel;
int yvel;
int frame;
int status;

public:
Char();
void handle_events();
void move();
void show();
void set_camera();
};


void Char::move()
{

//Move
box.x += xvel;

//Keep the stick figure in bounds
if( ( box.x < 0 ) || ( (box.x + CHAR_WIDTH) > LEVEL_WIDTH ) || ( check_collision( box, colli ) )) <--- so now here: i first hat a local but public variable from the function "Collision" instead of the global variable "colli"
{
box.x -= xvel;
}

box.y += yvel;

if( ( box.y < 0) || ( (box.y + CHAR_HEIGHT) > LEVEL_HEIGHT ) || ( check_collision( box, colli ) ))
{
box.y -= yvel;
}





class Collision
{

public:
//Constructor
Collision();
Collision(int, int, int, int);

void showcollision();

};

Collision::Collision()
{
colli.x = 470;
colli.y = 350;
colli.w = 40;
colli.h = 32;
}
Collision::Collision(int a, int b, int c, int d)
{
colli.x = a;
colli.y = b;
colli.w = c;
colli.h = d;
}





bool check_collision( SDL_Rect Char, SDL_Rect Collision )
{
//The sides of the rectangles
int leftChar, leftCollision;
int rightChar, rightCollision;
int topChar, topCollision;
int bottomChar, bottomCollision;

//Calculate the sides of rect A
leftChar = Char.x;
rightChar = Char.x + Char.w;
topChar = Char.y;
bottomChar = Char.y + Char.h;

//Calculate the sides of rect B
leftCollision = Collision.x;
rightCollision = Collision.x + Collision.w;
topCollision = Collision.y;
bottomCollision = Collision.y + Collision.h;

//If any of the sides from A are outside of B
if( bottomChar <= topCollision )
{
return false;
}

if( topChar >= bottomCollision )
{
return false;
}

if( rightChar <= leftCollision )
{
return false;
}

if( leftChar >= rightCollision )
{
return false;
}

//If none of the sides from A are outside B
return true;
}



and i was just wondering if there is a better way instead of using a global variable since i heard in objective programming you should use as less globals as possible to prevent errors in code etc.
isn't there an other way of passing a variable (or any other member) of a class to another class ?
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: a nooby class question ;)

Post by Ginto8 »

You could make the Char class a friend by putting this inside Collision:

Code: Select all

friend class Char;
This will allow member functions of Char to access private members of Collision. ;)
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
chrizZz
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 14
Joined: Tue Jan 27, 2009 5:12 am

Re: a nooby class question ;)

Post by chrizZz »

aha friend class .. i guess that is what i need !
unfortunately it doesn't work for me could you show me where exactly i have to write it in ? inside "private" or "public:" ? ^^; thx !

edit: don't bother i'll just read about friend classes, thank you anyways ! ;)
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: a nooby class question ;)

Post by Ginto8 »

chrizZz wrote:aha friend class .. i guess that is what i need !
unfortunately it doesn't work for me could you show me where exactly i have to write it in ? inside "private" or "public:" ? ^^; thx !

edit: don't bother i'll just read about friend classes, thank you anyways ! ;)
No problem. :mrgreen: And I'm pretty sure the friend class declaration goes in "private". ;)
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
User avatar
eatcomics
ES Beta Backer
ES Beta Backer
Posts: 2528
Joined: Sat Mar 08, 2008 7:52 pm
Location: Illinois

Re: a nooby class question ;)

Post by eatcomics »

Hey just to tell you, It's easier to put your code in the code boxes, just hit the code button above the post a reply box, and place your code between the ["code"] ["/code"] things... take out the quotes though.
Image
chrizZz
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 14
Joined: Tue Jan 27, 2009 5:12 am

Re: a nooby class question ;)

Post by chrizZz »

alright i'll keep in mind for the next time (im sure there will^^) thx 4 info !
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: a nooby class question ;)

Post by MarauderIIC »

Limit your use of friends. Implement a public accessor method in your class that returns the value of whatever variable you want.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
Post Reply