c++ - 简单 SDL 引擎中的链接错误

标签 c++ sdl

我正在尝试使用 C++ 和 SDL 构建一个简单的游戏引擎。我正在经历并尝试将我知道的东西放在一个大的 .cpp 文件中,并将它们组织成更合乎逻辑的模式。例如,有一个包含屏幕或引擎类、逻辑类、实体类等的游戏类。显然不需要 super 高级,因为我只是第一次尝试获取句柄逻辑地设置事物。不幸的是我收到链接器错误:

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.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

游戏.cpp

#include "game.h"

Game::Game()
{
    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

屏幕.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) );
        }
    }
}

感谢任何帮助!此外,如果有人对如何更好地组织我正在尝试做的事情有任何想法,请随时发表评论!

最佳答案

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

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

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

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

关于c++ - 简单 SDL 引擎中的链接错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22798441/

相关文章:

c - SDL 和 GLUT 之间的区别

c++ - 为什么编译器不能优化这个 std::string 结构?

c++ - 在每个系统日志 C++ 之前调用 openlog

c - SDL_net没有收到UDP数据包

c - SDL2 和 Xcode 的渲染器问题

c++ - 应用分发

c++ - 学习 C++ 和 SDL- 以下是否会产生内存泄漏?

c++ - gcc >=10.1 和 clang 不检测未使用但设置 lambda

c++ - 为什么会有注入(inject)的类名?

c++ - 使用错误的 maxsize 参数调用 strftime 会做什么?