Snake Game Tutorial 14a

The Partridge Family were neither partridges nor a family. Discuss.
albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Snake Game Tutorial 14a

Post by albinopapa » August 31st, 2017, 1:54 am

Lol, cheers
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

jresch95
Posts: 2
Joined: January 6th, 2018, 5:31 pm

Re: Snake Game Tutorial 14a

Post by jresch95 » January 6th, 2018, 6:17 pm

After spending a couple of hours struggling with errors, I decided to post to this thread to request assistance. Just after resolving to do so, I fixed my problem, so I thought I would post anyways in case someone else was having the same issue.

While working through tutorial 14b, I finally got the goal class defined and implemented, but upon building, I was met with several errors pointing to goal.h and stating:
missing type specifier - int assumed.
I checked and rechecked that all was correct in the goal.h, then turned to Google where I discovered from the following stack overflow post (https://stackoverflow.com/questions/232 ... nt-assumed) that the error is a result of something called circular dependency. This pointed me towards my various #include tags. After scouring the tutorial video and combing through the .h files, I realized that Chili's Snake.h file only had:

Code: Select all

#include "Board.h"
whereas my Snake.h file had:

Code: Select all

#include "Board.h"
#include "Location.h"
#include "Colors.h"
#include "Food.h"
The extra included header files were probably leftover from my efforts to figure out the snek game myself. Removing the extras immediately fixed all the errors.

I hope this helps anyone who may have struggled with this too. For those reading this who are more knowledgeable than I am, would you clarify why simply including unnecessary .h files causes the program to break? Furthermore, why does Chili only include Location.h in Board.h despite having location data members in many other header files? It makes sense to me that a header file should know about Location.h if they are going to have location objects. Or is it that they DO know about Location.h because all of them are included in Game.h? Thank you for your help!

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Snake Game Tutorial 14a

Post by albinopapa » January 6th, 2018, 9:27 pm

The header files are copy pasted everywhere they are included, and it is recursive. If Board.h includes Location.h, then any file that includes Board.h knows about the Location class. Which means Location.h is copied and pasted to Board.h and then Board.h is pasted to Snek.h with the Location.h contents. All of that is copied to the Snek.cpp file so that you can use the Location class and Board class inside Snek.

This should help figure out why circular dependencies wreck your program. If you include Snek.h in Food.h and Food.h in Snek.h, when does the compiler know when to quit making copies of each? Granted, the #pragma once should stop this behavior, but it doesn't.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

jresch95
Posts: 2
Joined: January 6th, 2018, 5:31 pm

Re: Snake Game Tutorial 14a

Post by jresch95 » January 7th, 2018, 5:43 am

That makes so much sense. Thank you very much!

ArmSun
Posts: 5
Joined: August 5th, 2018, 7:41 pm

Re: Snake Game Tutorial 14a

Post by ArmSun » August 15th, 2018, 6:48 pm

Hi
I'm trying to run snek game, and I'm getting an error at the disco part )
When I try to make a constructor for Board class, it gives an error
Graphics::Graphics(Graphics &) attempting to reference a deleted function

and the error line is
gfx(gfx)

I've looked into Graphics.h and found function declaration like this
Graphics( const Graphics& ) = delete;

Can anyone help me to move forward?

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Snake Game Tutorial 14a

Post by albinopapa » August 16th, 2018, 2:49 am

More than likely, you are trying to pass the gfx object by value. This would be a function like: Board::Draw( Graphics gfx ). You don't want a copy of the Graphics object gfx, you want a reference to the Graphics object: Board::Draw( Graphics& gfx ).
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

ArmSun
Posts: 5
Joined: August 5th, 2018, 7:41 pm

Re: Snake Game Tutorial 14a

Post by ArmSun » August 16th, 2018, 6:07 pm

This is the declaration of the function
Board(Graphics& gfx);

And here is it's definition

Board::Board(Graphics& gfx)
:
gfx(gfx)
{
}

Seems like I'm passing gfx by reference, not by value.

ArmSun
Posts: 5
Joined: August 5th, 2018, 7:41 pm

Re: Snake Game Tutorial 14a

Post by ArmSun » August 16th, 2018, 7:58 pm

Thank you for the reply. I've just found the error. I was declaring the constructor function in the right way, but the gxf parameter was not a reference, and that was causing the issue.

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Snake Game Tutorial 14a

Post by albinopapa » August 16th, 2018, 11:19 pm

Hey, glad you found the problem, good luck.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

ArmSun
Posts: 5
Joined: August 5th, 2018, 7:41 pm

Re: Snake Game Tutorial 14a

Post by ArmSun » October 2nd, 2018, 6:50 pm

Hi everyone, it's me again )

I'm having trouble with running snek game code. From time to time it gives me OUT OF VIDEO MEMORY error. I thought that I have done something wrong, so I've downloaded Chili's code from git. It gives the same error.

Can anyone help me?

Post Reply