views:

48

answers:

3

Linker Error:

$ make
g++ -Wall -g main.cpp SDL_Helpers.cpp Game.cpp DrawableObject.cpp `sdl-config --cflags --libs` -lSDL_mixer
/tmp/ccdxzrej.o: In function `Game':
/home/brett/Desktop/SDL/Game.cpp:16: undefined reference to `Player::Player(Game*)'
/home/brett/Desktop/SDL/Game.cpp:16: undefined reference to `Player::Player(Game*)'
collect2: ld returned 1 exit status
make: *** [all] Error 1

main.cpp:

#include "Game.h"

int main()
{
    Game g;
    return 0;
}

Game.h:

#ifndef BRETT_GAME_H
#define BRETT_GAME_H

#include <cmath>
#include "SDL.h"
#include "SDL_Helpers.h"
#include <vector>

class DrawableObject;

class Game
{
public:
    SDL_Surface * screen;
    std::vector<DrawableObject*> sprites;

    Game();
};

#endif

Game.cpp:

#include "Game.h"
#include "DrawableObject.h"
#include "Player.h"

Game::Game()
{   
    if (SDL_Init(SDL_INIT_VIDEO) != 0)
        return;

    atexit(SDL_Quit);
    screen = SDL_SetVideoMode(640, 480, 0, SDL_DOUBLEBUF);

    if (screen == NULL)
        return;

    sprites.push_back(new Player(this));

    while (true)
    {
        SDL_Event * event = NULL;
        while(SDL_PollEvent(event) && event)
        {
            if(event->type == SDL_QUIT)
                return;
        }

        SDL_LockSurface(screen);

        for (uint32 i=0; i<sprites.size(); ++i)
        {
            sprites[i]->update(event);
        }

        SDL_FreeSurface(screen);
        SDL_Flip(screen);
    }
}

DrawableObject.h:

#ifndef DRAWABLE_OBJECT_H
#define DRAWABLE_OBJECT_H

#include "SDL.h"
#include "SDL_Helpers.h"

class Game;

class DrawableObject
{
public:
    Game * const game;

    DrawableObject(Game * const game_);

    virtual void update(SDL_Event *event) = 0;

};

#endif

DrawableObject.cpp:

#include "DrawableObject.h"
#include "Game.h"

DrawableObject::DrawableObject(Game * const game_)
    :   game(game_)
{

}

Player.h:

#ifndef PLAYER_H
#define PLAYER_H

#include <cmath>
#include "DrawableObject.h"

class Player : public DrawableObject
{
public:
    Player(Game * const game);

    void update(SDL_Event *event);
};

#endif

Player.cpp:

#include "Player.h"
#include "Game.h"

Player::Player(Game * const game)
    : DrawableObject(game)
{

}

void Player::update(SDL_Event *event)
{
    draw_circle(game->screen, 100, 100, 50, 0xff000000);
}
+5  A: 

You're missing Player.cpp in your compilation line. You're having a link error.

Didier Trosset
It's also worth noting that there are no problems with the circular dependency.
Magnus Hoff
Forgot to add it to my makefile. Thanks.
zobdos
A: 

You just forgot to specify Player.cpp in the command line to g++. As a result, source file is not compiled into object file and linker cannot find the appropriate code definitions.

Vlad Lazarenko
A: 

Not a circular-dependency issue (at least, not as you've currently framed your question). You forgot to compile Player.cpp.

Oli Charlesworth