Homework #5 Help

The Partridge Family were neither partridges nor a family. Discuss.
Ziltwix
Posts: 39
Joined: January 3rd, 2017, 5:12 pm

Re: Homework #5 Help

Post by Ziltwix » February 16th, 2017, 4:48 pm

"A nonstatic member reference must be relative to a specific object."

I get this error because of the code above.

MrGodin
Posts: 721
Joined: November 30th, 2013, 7:40 pm
Location: Merville, British Columbia Canada

Re: Homework #5 Help

Post by MrGodin » February 16th, 2017, 6:20 pm

The code about will not work. To get the left right top bottom of the struct, you have to declare a struct like this

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
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 :)
Attachments
rect.png
rect.png (3.12 KiB) Viewed 3063 times
Curiosity killed the cat, satisfaction brought him back

MrGodin
Posts: 721
Joined: November 30th, 2013, 7:40 pm
Location: Merville, British Columbia Canada

Re: Homework #5 Help

Post by MrGodin » February 16th, 2017, 6:41 pm

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.
Curiosity killed the cat, satisfaction brought him back

Ziltwix
Posts: 39
Joined: January 3rd, 2017, 5:12 pm

Re: Homework #5 Help

Post by Ziltwix » February 16th, 2017, 8:47 pm

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 this

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
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 :)

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!" ?

Ziltwix
Posts: 39
Joined: January 3rd, 2017, 5:12 pm

Re: Homework #5 Help

Post by Ziltwix » February 16th, 2017, 8:50 pm

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?

MrGodin
Posts: 721
Joined: November 30th, 2013, 7:40 pm
Location: Merville, British Columbia Canada

Re: Homework #5 Help

Post by MrGodin » February 16th, 2017, 8:52 pm

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?
Yes, that is correct.
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!" ?
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 30 :)
Curiosity killed the cat, satisfaction brought him back

MrGodin
Posts: 721
Joined: November 30th, 2013, 7:40 pm
Location: Merville, British Columbia Canada

Re: Homework #5 Help

Post by MrGodin » February 16th, 2017, 8:59 pm

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.
Curiosity killed the cat, satisfaction brought him back

Ziltwix
Posts: 39
Joined: January 3rd, 2017, 5:12 pm

Re: Homework #5 Help

Post by Ziltwix » February 17th, 2017, 2:27 am

Thank you, MrGodin!! :D

MrGodin
Posts: 721
Joined: November 30th, 2013, 7:40 pm
Location: Merville, British Columbia Canada

Re: Homework #5 Help

Post by MrGodin » February 17th, 2017, 3:11 am

Hope i helped
Curiosity killed the cat, satisfaction brought him back

Ziltwix
Posts: 39
Joined: January 3rd, 2017, 5:12 pm

Re: Homework #5 Help

Post by Ziltwix » February 18th, 2017, 5:11 pm

MrGodin! How do we return values?

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;
	
	}
}
This code says that, "Game::collide must return a value."

What does that mean actually?

Post Reply