Snake Class Question

The Partridge Family were neither partridges nor a family. Discuss.
purpleclover
Posts: 7
Joined: June 10th, 2018, 12:09 pm

Snake Class Question

Post by purpleclover » June 19th, 2018, 11:48 am

Hello Everyone,

iv been trying to do the snake game on my own and iv hit a dead end in the inizialization of the "Snake" Class, im getting the following Compilation error:

error C3646: 'brd': unknown override specifier (compiling source file Snake.cpp)

I dont know why that pops up, could u guys check out my code and help me out please?

Thanks alot!
Attachments
test.7z
(225.49 KiB) Downloaded 129 times

purpleclover
Posts: 7
Joined: June 10th, 2018, 12:09 pm

Re: Snake Class Question

Post by purpleclover » June 19th, 2018, 11:56 am

Ah damn it guys, im sorry for the Spam.
Found my problem...

Forgot to add #include "Board.h" in the "Snake.h" Header file :(

User avatar
Yumtard
Posts: 575
Joined: January 19th, 2017, 10:28 pm
Location: Idiot from northern Europe

Re: Snake Class Question

Post by Yumtard » June 19th, 2018, 12:00 pm

I don't think board should be a member of the snake class. You would just need it to draw the snake so just pass it as a reference to the draw function.

Also you're including board.h in the snakes cpp file. If you want it as a member variable for whatever reason you need to include board in snakes h file. Unless you just wanna cache a pointer to the board, then you can forward declare it

purpleclover
Posts: 7
Joined: June 10th, 2018, 12:09 pm

Re: Snake Class Question

Post by purpleclover » June 19th, 2018, 12:15 pm

Thanks alot for your quick response Yumtard!

That Tip with the "pass it as a reference" is awesome!
I implemented it and also removed the obsolte include.

Love you guys.

purpleclover
Posts: 7
Joined: June 10th, 2018, 12:09 pm

Re: Snake Class Question

Post by purpleclover » June 19th, 2018, 1:03 pm

Iv already hit another "wall", i think iv gotten myself in a circular dependancy Problem.

Could you guys take a look at my code and help me out?

Thanks alot!
Attachments
test.7z
(225.51 KiB) Downloaded 126 times

User avatar
Yumtard
Posts: 575
Joined: January 19th, 2017, 10:28 pm
Location: Idiot from northern Europe

Re: Snake Class Question

Post by Yumtard » June 19th, 2018, 1:32 pm

Well you COULD solve it by passing snake as a pointer and do forward declaration. However, in this case you shouldn't.

You're including snake in board just to send it to the draw function where you use the snake to get its position. Why not just pass the position to the function?

DrawBoard(Graphics& gfx, Vec2& pos);

User avatar
Yumtard
Posts: 575
Joined: January 19th, 2017, 10:28 pm
Location: Idiot from northern Europe

Re: Snake Class Question

Post by Yumtard » June 19th, 2018, 1:38 pm

also you will then want apple positions, obstacle positions and what not, so rather than sending a bunch of things to a DrawBoard() you could just have a DrawCell(const vec& pos)

Then in Snake:Draw() you loop over the snakes segments and call the boards draw function on their positions. And do the same thing in Obstacle::Draw() and Apple::Draw()

purpleclover
Posts: 7
Joined: June 10th, 2018, 12:09 pm

Re: Snake Class Question

Post by purpleclover » June 19th, 2018, 1:56 pm

Thanks again Yumtard, uv helped me alot.

User avatar
Yumtard
Posts: 575
Joined: January 19th, 2017, 10:28 pm
Location: Idiot from northern Europe

Re: Snake Class Question

Post by Yumtard » June 19th, 2018, 1:56 pm

No problem! Gl with the rest of the game :)

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: Snake Class Question

Post by chili » June 19th, 2018, 3:43 pm

yeh good advice, pass around the minimal info necessary. Minimize dependencies and coupling.
Chili

Post Reply