简单SDL引擎中的链接错误

时间:2014-04-01 22:53:22

标签: c++ sdl

我正在尝试使用C ++和SDL构建一个简单的游戏引擎。我正在尝试将我知道的东西放在一个大的.cpp文件中,并将它们组织成更合理的模式。例如,拥有一个Game类,它包含一个Screen或Engine类,一个Logic类,一个Entities类等。显然,我不需要超级先进,因为我只是试图获得一个句柄。第一次在逻辑上设置东西。不幸的是我收到链接器错误:

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

SDL肯定是正确设置的,因为当一切都在一个大文件中时,代码执行得很好。

到目前为止,我有一个Game类和一个Screen类。

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) );
        }
    }
}

任何帮助表示赞赏!此外,如果有人对如何更好地组织我想要做的事情有任何想法,请随时发表评论!

1 个答案:

答案 0 :(得分:2)

由于您已经为Screen和Game声明了析构函数,因此您也需要为它们提供定义。在Game.cpp和Screen.cpp文件中添加:

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

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

如果你没有声明析构函数,编译器会为你隐式生成。