Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Keep seeing: error: expected ')' before '&' token ?  (Read 4337 times)

0 Members and 1 Guest are viewing this topic.

bglaze

  • Newbie
  • *
  • Posts: 46
    • View Profile
Keep seeing: error: expected ')' before '&' token ?
« on: October 11, 2011, 07:48:12 pm »
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:
Code: [Select]

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
Code: [Select]

#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
Code: [Select]

#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
Code: [Select]

#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
Code: [Select]

#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
Code: [Select]

#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

asdatapel

  • Jr. Member
  • **
  • Posts: 76
    • View Profile
Keep seeing: error: expected ')' before '&' token ?
« Reply #1 on: October 11, 2011, 08:18:23 pm »
i think you have to delete the protected..but im not sure..

bglaze

  • Newbie
  • *
  • Posts: 46
    • View Profile
Keep seeing: error: expected ')' before '&' token ?
« Reply #2 on: October 11, 2011, 08:22:30 pm »
I hadn't thought of that, but I tried it, and it did not work.

Thanks for the suggestion, though.

Disch

  • Full Member
  • ***
  • Posts: 220
    • View Profile
Keep seeing: error: expected ')' before '&' token ?
« Reply #3 on: October 11, 2011, 08:37:05 pm »
Circular dependency.

Game.h is including Snake.h
Snake.h is including Game.h

Read section 4 (for how to fix/avoid) and 6 (for explanation of why it's happening) of this article:

http://www.cplusplus.com/forum/articles/10627/#msg49679

Long story short, you need to break the circular dependency by removing one of the includes and replacing them with a forward declaration.

bglaze

  • Newbie
  • *
  • Posts: 46
    • View Profile
Keep seeing: error: expected ')' before '&' token ?
« Reply #4 on: October 11, 2011, 08:38:37 pm »
Disch,

that would explain why it keeps happening. =)

Thanks so much!

bglaze

  • Newbie
  • *
  • Posts: 46
    • View Profile
Keep seeing: error: expected ')' before '&' token ?
« Reply #5 on: October 11, 2011, 09:49:10 pm »
Disch,

Thanks for helping a newb out. That advice will continue to help me forever. I read that entire tutorial on Headers, and it cleared up exactly what I was doing.

Thanks again,
Brock

Disch

  • Full Member
  • ***
  • Posts: 220
    • View Profile
Keep seeing: error: expected ')' before '&' token ?
« Reply #6 on: October 11, 2011, 10:25:15 pm »
Glad to help.

sbroadfoot90

  • Jr. Member
  • **
  • Posts: 77
    • View Profile
Keep seeing: error: expected ')' before '&' token ?
« Reply #7 on: October 12, 2011, 12:11:48 am »
Another problem

Code: [Select]
class Snake
{
    public:
        Snake(Game& g);
    protected:
    private:
    Game game;
};


you should store a reference to game, not a copy.