a nooby class question ;)
Moderator: Coders of Rage
a nooby class question ;)
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 ? ^^
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 ? ^^
Re: a nooby class question ;)
I'm not sure I understand, you should post some code. Include the classes and the where you are calling the function.
- Falco Girgis
- 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 ;)
Yeah, post your code. There's a clear misunderstanding about how passing parameters to functions work.
Re: a nooby class question ;)
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 ?
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 ?
- Ginto8
- 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 ;)
You could make the Char class a friend by putting this inside Collision:
This will allow member functions of Char to access private members of Collision.
Code: Select all
friend class Char;
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.
Re: a nooby class question ;)
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 !
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 !
- Ginto8
- 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 ;)
No problem. And I'm pretty sure the friend class declaration goes in "private".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 !
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.
Re: a nooby class question ;)
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.
Re: a nooby class question ;)
alright i'll keep in mind for the next time (im sure there will^^) thx 4 info !
- MarauderIIC
- Respected Programmer
- Posts: 3406
- Joined: Sat Jul 10, 2004 3:05 pm
- Location: Maryland, USA
Re: a nooby class question ;)
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.