Domanda

I am trying to build a simple game engine with C++ and SDL. I am going through and trying to take the things that I know work in one big .cpp file, and organizing them into more logical patterns. For example, having a Game class, that contains a Screen or Engine class, a Logic class, an Entities class, etc. Obviously doesn't need to be super advanced as I'm just trying to get a handle for the first time on setting up things logically. Unfortunately I get the linker errors:

1>game.obj : error LNK2019: unresolved external symbol "public: __thiscall Screen::~Screen(void)" (??1Screen@@QAE@XZ) referenced in function "public: __thiscall Game::Game(void)" (??0Game@@QAE@XZ)
1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall Game::~Game(void)" (??1Game@@QAE@XZ) referenced in function _SDL_main

And the SDL is definitely set up properly, because the code executes just fine when everything is in one big file.

So far I have a Game class, and a Screen class.

game.h

#ifndef GAME_H
#define GAME_H

// Includes
#include "screen.h"

// SDL specific includes
#include "SDL.h"

class Game
{
private:
    Screen screen;
public:
    Game();
    ~Game();
};

#endif

game.cpp

#include "game.h"

Game::Game()
{
    Screen screen;
}

screen.h

#ifndef SCREEN_H
#define SCREEN_H

#include "SDL.h"

class Screen
{
private:
    // Screen dimension variables
    int SCREEN_WIDTH, SCREEN_HEIGHT;

    // --SDL object variables--
    // The window we'll be rendering to
    SDL_Window * window;
    // The surface contained by the window
    SDL_Surface * screenSurface;
public:
    Screen();
    ~Screen();

    // Functions for calling SDL objects
    SDL_Window *getSDLWindow( void );
    SDL_Surface *getSDLSurface( void );
};

#endif

screen.cpp

#include "screen.h"
#include <stdio.h>

Screen::Screen()
{
    // Initialize window and SDL surface to null
    window = NULL;
    screenSurface = NULL;
    // Initialize screen dimensions
    SCREEN_WIDTH = 800;
    SCREEN_HEIGHT = 600;

    // Initialize SDL
    if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
    {
        printf("SDL could not initialize! SDL Error: %s\n", SDL_GetError() );
    }
    else
    {
        // Create a window
        window = SDL_CreateWindow( "The First Mover", SDL_WINDOWPOS_UNDEFINED,    SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
        if (window == NULL)
        {
            printf ("Window could not be created! SDL Error: %s\n", SDL_GetError() );
        }
        else
        {
            // Get window surface
            screenSurface = SDL_GetWindowSurface( window );

            // Fill the surface white
            SDL_FillRect( screenSurface, NULL, SDL_MapRGB( screenSurface->format, 0xFF, 0xFF, 0xFF) );
        }
    }
}

Any assistance appreciated! Also, if anyone has any ideas on how to better organize what I'm trying to do, feel free to comment!

È stato utile?

Soluzione

Since you have declared the destructors for Screen and Game you need to provide definitions for them too. Add this in the Game.cpp and Screen.cpp files:

Game::~Game(){
    // what ever you need in the destructor
}

Screen::~Screen(){
    // what ever you need in the destructor
}

If you hadn't declared the destructors the compiler would have generated an implicitly for you.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top