I keep seeing the following error when trying to assign my game object to my snake object. Since I have seen it so many times now, and have spent 2 days finding workarounds, but without knowing what is causing it, I am hoping someone can see why it's happening, so I can fix it directly.
Here is the full error:
s Files\CPP\Dev\The_Yellow_Snake\Yellow_Snake_testing_window_passing\Snake.h|12|error: expected ')' before '&' token|
s Files\CPP\Dev\The_Yellow_Snake\Yellow_Snake_testing_window_passing\Snake.h|15|error: 'Game' does not name a type|
s Files\CPP\Dev\The_Yellow_Snake\Yellow_Snake_testing_window_passing\Snake.cpp|6|error: prototype for 'Snake::Snake(Game&)' does not match any in class 'Snake'|
s Files\CPP\Dev\The_Yellow_Snake\Yellow_Snake_testing_window_passing\Snake.h|9|error: candidates are: Snake::Snake(const Snake&)|
s Files\CPP\Dev\The_Yellow_Snake\Yellow_Snake_testing_window_passing\Snake.h|9|error: Snake::Snake()|
||=== Build finished: 5 errors, 0 warnings ===|
And here are my files, starting with main.cpp
#include "Game.h"
#include "Snake.h"
#include <SFML/Graphics.hpp>
#include <iostream>
int main()
{
sf::VideoMode VMode(600, 390, 32);
sf::RenderWindow window;
window.Create(VMode, "Yellow Snake", sf::Style::Close);
Game game(window);
game.Run();
return 0;
}
Game.h
#ifndef GAME_H
#define GAME_H
#include "Game.h"
#include "Snake.h"
#include <SFML/Graphics.hpp>
#include <iostream>
class Game
{
public:
Game(sf::RenderWindow& window);
//funcs
void Run();
//vars
double FPS;
sf::RenderWindow& Window;
protected:
private:
};
#endif // GAME_H
Game.cpp
#include "Game.h"
#include "Snake.h"
#include <SFML/Graphics.hpp>
#include <iostream>
Game::Game(sf::RenderWindow& window):
FPS(15.0),
Window(window)
{
}
void Game::Run()
{
Snake snake(this);
while(Window.IsOpened())
{
sf::Event event;
while(Window.GetEvent(event))
{
switch(event.Type)
{
case sf::Event::Closed:
Window.Close();
break;
default:
break;
}
}
Window.Clear(sf::Color(0, 0, 0));
Window.Display();
/* Free up the CPU */
sf::Sleep(1.0/FPS);
}
}
Snake.h
#ifndef SNAKE_H
#define SNAKE_H
#include "Game.h"
#include "Snake.h"
#include <SFML/Graphics.hpp>
#include <iostream>
class Snake
{
public:
Snake(Game& g);
protected:
private:
Game game;
};
#endif // SNAKE_H
Snake.cpp
#include "Game.h"
#include "Snake.h"
#include <SFML/Graphics.hpp>
#include <iostream>
Snake::Snake(Game& g):
game(g)
{
}
My full program is larger, but this is as small as I could get it and reproduce the error.
Your consideration is much appreciated!
Thanks
- Brock