Thread: C++ SDL: Undefined reference to Game::Game()?

  1. #1
    Registered User
    Join Date
    Feb 2013
    Location
    Sweden
    Posts
    171

    C++ SDL: Undefined reference to Game::Game()?

    I'm reading the book SDL Game Development. And where going to do this step. And then I'm got stuck my compiler complained about that Game::Game() was undefined? And I'm not really sure what's wrong?

    I know that I have both main.cpp and Game.h in the same directory so it cannot be that, that is wrong.

    What does a "undefined reference" error even mean?


    The main.cpp file
    Code:
    #include <iostream>
    #include "SDL.h"
    #include "Game.h"
    
    
    using namespace std;
    
    
    Game *g_game = 0;
    
    
    int main(int argc, char* argv[])
    {
        g_game = new Game();
        return 0;
    }
    The Game.h file
    Code:
    #ifndef GAME_H_INCLUDED
    #define GAME_H_INCLUDED
    
    
    class Game
    {
        public:
            Game();
            ~Game();
    
    
            // Initialize
            void init() {m_bRunning = true;}
    
    
    
    
            void render() {}
            void update() {}
            void handleEvents() {}
    
    
            // To access the private running variable
            bool running() {return m_bRunning;}
    
    
        private:
            bool m_bRunning;
    };
    
    
    #endif // GAME_H_INCLUDED

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Where did you define the member functions of the Game class? Maybe it is that source file that was not included in the project, or otherwise compiled and linked.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2013
    Location
    Sweden
    Posts
    171
    In the main.cpp file. Is all of the member functions of the Game class. But I find that I don't have to use yet: g_game = new Game(); But however I have included the member functions in the main.cpp file. Should I create a Game.cpp file for the constructor and deconstructor? To initialize them?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by DecoratorFawn82
    In the main.cpp file. Is all of the member functions of the Game class.
    Since you did not show that part of your main.cpp file, I find it plausible that you may be mistaken, e.g., you forgot to define the default constructor.

    Quote Originally Posted by DecoratorFawn82
    But I find that I don't have to use yet: g_game = new Game();
    What do you mean?

    Quote Originally Posted by DecoratorFawn82
    Should I create a Game.cpp file for the constructor and deconstructor? To initialize them?
    We say "define", not "initialize" here. Also, "destructor", not "deconstructor". Anyway, I would likely have a separate game.cpp file for all the member function definitions of the Game class (at least those member functions that were not defined inline in the class definition), not just the constructor and destructor.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    What does a "undefined reference" error even mean?
    If you don't know what that might be telling you then i think you might be running before you can walk.

    The definition of the constructor (or other functions) is the working code that defines what your function does - You could if you really wanted write it into the class without adding it to another source file, i would not do that very often personally but you do see this in tutorials about class basics for example. Also a constructor is special in that if you don't define one then a default is provided for you.
    Last edited by rogster001; 09-24-2013 at 12:04 PM.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  6. #6
    Registered User
    Join Date
    Feb 2013
    Location
    Sweden
    Posts
    171
    OK thanks! And sorry if I'm used wrong words or spelled any words wrong. But you got what I mean however

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Should I use a game engine in making a RPG game?
    By m3rk in forum Game Programming
    Replies: 6
    Last Post: 01-26-2009, 04:58 AM
  2. Guessing game: how to quit the game?
    By hzr in forum C Programming
    Replies: 5
    Last Post: 12-18-2008, 10:53 AM
  3. craps game & dice game..
    By cgurl05 in forum C Programming
    Replies: 3
    Last Post: 03-25-2006, 07:58 PM
  4. Game Designer vs Game Programmer
    By the dead tree in forum Game Programming
    Replies: 8
    Last Post: 04-28-2005, 09:17 PM
  5. Reference to making breakout game
    By Raison in forum Game Programming
    Replies: 10
    Last Post: 12-31-2004, 05:18 PM