Page 5 of 7

Re: Snake Game Tutorial 14a

Posted: April 13th, 2017, 3:46 pm
by Adagio
chili wrote:You're missing the default constructor for the segment

You want like Segment() = default;
Where do i put the default constructor for the segment? I wrote it as Segment() = default; under the segment class and it still has the same error of trying to reference a deleted function.

1>c:\users\Adagio\desktop\t14-snek-start\engine\snake.cpp(5): error C2280: 'Snake::Segment::Segment(void)': attempting to reference a deleted function
1>c:\users\Adagio\desktop\t14-snek-start\engine\snake.h(12): note: see declaration of 'Snake::Segment::Segment'

Re: Snake Game Tutorial 14a

Posted: April 13th, 2017, 5:45 pm
by albinopapa
Adagio wrote:
chili wrote:You're missing the default constructor for the segment

You want like Segment() = default;
Where do i put the default constructor for the segment? I wrote it as Segment() = default; under the segment class and it still has the same error of trying to reference a deleted function.

1>c:\users\Adagio\desktop\t14-snek-start\engine\snake.cpp(5): error C2280: 'Snake::Segment::Segment(void)': attempting to reference a deleted function
1>c:\users\Adagio\desktop\t14-snek-start\engine\snake.h(12): note: see declaration of 'Snake::Segment::Segment'
I'm going to say that this is the culprit
note: 'Snake::Segment::Segment(void)': function was implicitly deleted because a data member 'Snake::Segment::loc' has either no appropriate default constructor or overload resolution was ambiguous
You probably did the same as someone else in the forum and make a constructor for the Location struct. Remove any Location::Location constructors, and you should be ok.

Re: Snake Game Tutorial 14a

Posted: April 13th, 2017, 5:51 pm
by albinopapa
Here's a link to the previous persons experience with same issue
http://planetchili.net/forum/viewtopic. ... 7&start=10

Re: Snake Game Tutorial 14a

Posted: April 14th, 2017, 2:31 am
by chili
Ah right, I was looking for that! Thanks for posting the link. ;)

Re: Snake Game Tutorial 14a

Posted: April 14th, 2017, 3:26 am
by Adagio
Right I removed the constructor i made for the location struct and it built successfully. Thanks for the help guys!

Re: Snake Game Tutorial 14a

Posted: April 14th, 2017, 2:22 pm
by Adagio
Hello again my friends!
I've finished tutorial 14b and was trying out the homework section and I have a question regarding the spawning of the obstacle.
I looked through the github file Chili put up in the wiki and there's several areas I'm unclear on.
First, what happens when u set a boolean statement to an array ( e.g. bool hasObstacle[width * height] = { false }; )? does it set up a boolean for each cell in the grid?

Secondly, the boolean function you have set up in board.cpp :
bool Board::CheckForObstacle( const Location & loc ) const
{
return hasObstacle[loc.y * width + loc.x];
}


why do you take loc.y * width + loc.x inside the array number an what does that statement return exactly?

I'm guessing you are checking each cell in the grid to see if there is an obstacle in the grid and to draw 1 if there isn't according to the draw obstacle function. I just don't get what this line in particular: "return hasObstacle[loc.y * width + loc.x];" mean. :?

Re: Snake Game Tutorial 14a

Posted: April 14th, 2017, 9:59 pm
by albinopapa
First, what happens when u set a boolean statement to an array ( e.g. bool hasObstacle[width * height] = { false }; )? does it set up a boolean for each cell in the grid?
Kind of, it makes an array with the same number of bool elements as the grid.
Secondly, the boolean function you have set up in board.cpp :
bool Board::CheckForObstacle( const Location & loc ) const
{
return hasObstacle[loc.y * width + loc.x];
}

why do you take loc.y * width + loc.x inside the array number an what does that statement return exactly?
If you have made it this far in the tutorials, you would have covered functions. Look at the function declaration and it tells you want it returns.
As far as ( loc.y * width + loc.x ) goes, this is covered in one of the tutorials, but I don't remember if chili covered it at that point or not. Each row is width wide, so loc.y being the vertical axis ( row number ), when you multiply the row number by the number of columns, you get an offset to the beginning of that row. When you add loc.x to it, you get the location of the cell in the array.

Imagine you have a 10x10 grid
0 1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18 19
20 21 22 23 24 25 26 27 28 29
30 31 32 33 34 35 36 37 38 39
...

If you want to know the index of the cell in the array you want, you multiply row number by the width of the grid.
Example ( x = 3, y = 2, width = 10 )
row_offset = y * width // 2 * 10 = 20
index = x + row_offset // 20 + 3 = 23
So now
return hasObstacle[loc.y * width + loc.x];
would return a bool from the array hasObstacle at index 23.

Re: Snake Game Tutorial 14a

Posted: April 15th, 2017, 3:29 am
by Adagio
I see. So ( loc.y * width + loc.x ) is the input of the individual coordinates of the cell in the grid. I was having trouble visualising what it meant but now i understand. Thanks for the explanation @albinopapa!
So bool CheckForObstacle(const Location& loc) const; just returns the bool element of a specific coordinate { x, y } in the grid!

Re: Snake Game Tutorial 14a

Posted: April 15th, 2017, 6:09 am
by albinopapa
@Adagio
Yes, it returns the bool element at the index of hasObstacle specified by the Location loc object with the values stored at members x and y.

Re: Snake Game Tutorial 14a

Posted: August 31st, 2017, 12:08 am
by Geezer
I got thus same error. Even with the default constructor added. I ended up taking off the = default and created a body for the constructor where I just set the value of c = Colors::Cyan;
The error moved to Location has no default and I added one.
Location() = default;

I'm still a little confused why a couple of us got these errors and not everyone. But in the end, if it compiles ok, I'll live with it.

Geezer.

Aaaaaand Now I just found all the other posts on here. I do have a Location constructor, so there's the problem. Thanks.