Homework #5 Help
Re: Homework #5 Help
"A nonstatic member reference must be relative to a specific object."
I get this error because of the code above.
I get this error because of the code above.
Re: Homework #5 Help
The code about will not work. To get the left right top bottom of the struct, you have to declare a struct like this
does this help ?
Note: when making a rectangle you should (in your case) make sure that right is greater than left
and bottom is greater than top
ie: left = 10, right = 20
and the same for top and bottom
top = 10, bottom = 20.
Left and right are X values, Top and bottom are Y values
Code: Select all
Rectangle A;// declares a rectangle named A.You can name it whatever you want
// You can make as many Rectangles as you want, you just have to give them different names.
// To set the the member variables ie: left,right ect
// you go
A.left = 100; as an example
A.right = 120;
A.top = 90;
A.bottom = 120;
// To access the variables you go
int leftA = A.left;
int topA = A.top ect..
// or use it like this
if(A.left == 100)
{
//do something..
|
// you can also make a copy of A by going Rectangle B = A, B will be exactly the same as A
Note: when making a rectangle you should (in your case) make sure that right is greater than left
and bottom is greater than top
ie: left = 10, right = 20
and the same for top and bottom
top = 10, bottom = 20.
Left and right are X values, Top and bottom are Y values
- Attachments
-
- rect.png (3.12 KiB) Viewed 3063 times
Curiosity killed the cat, satisfaction brought him back
Re: Homework #5 Help
A struct is almost a class but not quite. You can make structs (structures) of your very own and called them whatever you want.
example
struct Positions
{
int x0;
int y0;
int x1;
int y1;
// you can add as many variables as you want and different types as well
float distance;
}
Then you again declare a Positions struct like .. Positions ObjectPositions; (again named whatever you want)
Then again go ObjectPositions.x0 = 20, for instance
In your file i can see how i confused you by going
struct Rectangle
{
int left,top,right,bottom;
}
doing it like that is the same as going
struct Rectangle
{
int left;
int right;
int top;
int bottom;
}
The order in which you place left,top,right and bottom does not matter really, it's up to you how you make stuctures.
example
struct Positions
{
int x0;
int y0;
int x1;
int y1;
// you can add as many variables as you want and different types as well
float distance;
}
Then you again declare a Positions struct like .. Positions ObjectPositions; (again named whatever you want)
Then again go ObjectPositions.x0 = 20, for instance
In your file i can see how i confused you by going
struct Rectangle
{
int left,top,right,bottom;
}
doing it like that is the same as going
struct Rectangle
{
int left;
int right;
int top;
int bottom;
}
The order in which you place left,top,right and bottom does not matter really, it's up to you how you make stuctures.
Curiosity killed the cat, satisfaction brought him back
Re: Homework #5 Help
MrGodin wrote:The code about will not work. To get the left right top bottom of the struct, you have to declare a struct like thisdoes this help ?Code: Select all
Rectangle A;// declares a rectangle named A.You can name it whatever you want // You can make as many Rectangles as you want, you just have to give them different names. // To set the the member variables ie: left,right ect // you go A.left = 100; as an example A.right = 120; A.top = 90; A.bottom = 120; // To access the variables you go int leftA = A.left; int topA = A.top ect.. // or use it like this if(A.left == 100) { //do something.. | // you can also make a copy of A by going Rectangle B = A, B will be exactly the same as A
Note: when making a rectangle you should (in your case) make sure that right is greater than left
and bottom is greater than top
ie: left = 10, right = 20
and the same for top and bottom
top = 10, bottom = 20.
Left and right are X values, Top and bottom are Y values
So when you set the member variable, you are basically telling that part of whatever rectangle that it's value is: 40, 50 or anything of that nature?
Isn't accessing this member variable (int leftA = A.left;) kind of like telling the program, "Hey man, this is what that part of the rectangle is called!" ?
Re: Homework #5 Help
MrGodin wrote:A struct is almost a class but not quite. You can make structs (structures) of your very own and called them whatever you want.
example
struct Positions
{
int x0;
int y0;
int x1;
int y1;
// you can add as many variables as you want and different types as well
float distance;
}
Then you again declare a Positions struct like .. Positions ObjectPositions; (again named whatever you want)
Then again go ObjectPositions.x0 = 20, for instance
In your file i can see how i confused you by going
struct Rectangle
{
int left,top,right,bottom;
}
doing it like that is the same as going
struct Rectangle
{
int left;
int right;
int top;
int bottom;
}
The order in which you place left,top,right and bottom does not matter really, it's up to you how you make stuctures.
Is this (Positions ObjectPositions;) suppose to ensure that the struct is working alright? Because this is what got me. The "Object" part must be put in any part of a struct member that we create?
Re: Homework #5 Help
Yes, that is correct.So when you set the member variable, you are basically telling that part of whatever rectangle that it's value is: 40, 50 or anything of that nature?
int leftA = A.left is setting leftA to equal the value of A.left say it's 30, so A.left keeps it's value of 30 and now leftA also equals 30Isn't accessing this member variable (int leftA = A.left;) kind of like telling the program, "Hey man, this is what that part of the rectangle is called!" ?
Curiosity killed the cat, satisfaction brought him back
Re: Homework #5 Help
Positions is the struct of data, so when you want to use that struct you make an instance of it. In that example i just named it ObjectPositions. The name is completely arbitrary.
You can go
Positions DogPositions. Now DogPositions has all the variables of Positions ie:
DogPositions.x0 = 30;
DogPositions.x1 = 40 ect.
You can go
Positions DogPositions. Now DogPositions has all the variables of Positions ie:
DogPositions.x0 = 30;
DogPositions.x1 = 40 ect.
Curiosity killed the cat, satisfaction brought him back
Re: Homework #5 Help
MrGodin! How do we return values?
This code says that, "Game::collide must return a value."
What does that mean actually?
Code: Select all
bool Game::collide(Rectangle A, Rectangle B)
{
if (A.top < B.bottom && A.bottom > B.top &&
A.left < B.right && A.right > B.left)
{
gb = 0;
}
else
{
gb = 255;
}
}
What does that mean actually?