2014-04-01 37 views
0

我想用C++和SDL構建一個簡單的遊戲引擎。我正在經歷並嘗試將我所知道的事情放在一個大的.cpp文件中,並將它們組織成更多的邏輯模式。例如,有一個Game類,它包含一個Screen或Engine類,一個Logic類,一個Entities類等等。顯然不需要超級先進,因爲我只是試圖首次獲得句柄邏輯地設置事物。不幸的是我得到的鏈接錯誤:簡單SDL引擎中的鏈接錯誤

1>game.obj : error LNK2019: unresolved external symbol "public: __thiscall Screen::~Screen(void)" ([email protected]@[email protected]) referenced in function "public: __thiscall Game::Game(void)" ([email protected]@[email protected]) 
1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall Game::~Game(void)" ([email protected]@[email protected]) referenced in function _SDL_main 

而SDL肯定是設置正確,因爲代碼執行就好了,當一切都在一個大文件。

到目前爲止,我有一個遊戲類和一個屏幕類。

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

任何幫助讚賞!此外,如果有人對如何更好地組織我想要做的事情有任何想法,請隨時發表評論!

回答

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 
} 

如果你沒有申報,編譯器會生成一個隱適合你的析構函數。

+0

太棒了!我習慣性地把它們放在那裏,並沒有考慮需要定義它們。謝謝你,朋友。像魅力一樣工作。 – Lapys