【问题标题】:LNK2019 Error when calling methods of a static member in .cpp fileLNK2019 调用 .cpp 文件中静态成员的方法时出错
【发布时间】:2018-09-02 19:55:23
【问题描述】:

我的代码中出现了这些错误,它们都与我的 Game 类中的一个静态成员有关。

Game.obj : error LNK2019: unresolved external symbol "public: class std::shared_ptr<class MainMenuScene> __thiscall SceneManager::AddScene<class MainMenuScene>(void)" (??$AddScene@VMainMenuScene@@@SceneManager@@QAE?AV?$shared_ptr@VMainMenuScene@@@std@@XZ) referenced in function "public: void __thiscall Game::init(char const *,int,int,bool)" (?init@Game@@QAEXPBDHH_N@Z)
Game.obj : error LNK2019: unresolved external symbol "public: class std::shared_ptr<class GameSelectScene> __thiscall SceneManager::AddScene<class GameSelectScene>(void)" (??$AddScene@VGameSelectScene@@@SceneManager@@QAE?AV?$shared_ptr@VGameSelectScene@@@std@@XZ) referenced in function "public: void __thiscall Game::init(char const *,int,int,bool)" (?init@Game@@QAEXPBDHH_N@Z)
Game.obj : error LNK2019: unresolved external symbol "public: class std::shared_ptr<class MainMenuScene> __thiscall SceneManager::ChangeScene<class MainMenuScene>(void)" (??$ChangeScene@VMainMenuScene@@@SceneManager@@QAE?AV?$shared_ptr@VMainMenuScene@@@std@@XZ) referenced in function "public: void __thiscall Game::init(char const *,int,int,bool)" (?init@Game@@QAEXPBDHH_N@Z)

在我的 game.h 中我有:

#pragma once

class SceneManager; 

using namespace std;

class Game {
public:
    Game();
    ~Game();

    void init(const char* title, int width, int height, bool fullscreen);
    void handleEvents();
    void update();
    void render();
    void clean();

    bool isRunning() { return running; }

    static SDL_Renderer * renderer; 
    static SceneManager * sceneManager; 
};

错误发生在我的 game.cpp 文件的 init() 方法中,我的 game.cpp 看起来像:

#include "Game.h"

#include "SceneManager.h"

#include "GameSelectScene.h"
#include "MainMenuScene.h" 

SceneManager * Game::sceneManager = new SceneManager();

Game::Game() {}
Game::~Game() {}

void Game::init(const char* title, int width, int height, bool fullscreen) {
    // I do some stuff up here

    // Here is where I think errors are happening.
    sceneManager->AddScene<MainMenuScene>();
    sceneManager->AddScene<GameSelectScene>();

    sceneManager->ChangeScene<MainMenuScene>();
}

我的 GameSelectScene.h 和 MainMenuScene.h 都是我的 Scene.h 的子类 我的 SceneManager 是定义 AddScene 和 ChangeScene 方法的地方

我的 SceneManager.h: #pragma 一次

#include "Game.h"
#include "Scene.h"
#include <map>
#include <string>
#include <typeindex>

using namespace std;

class SceneManager {
public:
    SceneManager();
    ~SceneManager();

    void init();
    void update();
    void render();

    template <typename T> std::shared_ptr<T> ChangeScene();
    template <typename T> std::shared_ptr<T> AddScene();

private:
    std::map<type_index, std::shared_ptr<Scene>> scenes;
    std::shared_ptr<Scene> currentScene;
}; 

SceneManager.cpp:

#include "SceneManager.h"

SceneManager::SceneManager() {}
SceneManager::~SceneManager() {}

// I define the init() update() and render()

template <typename T> std::shared_ptr<T> SceneManager::ChangeScene() {
    type_index index(typeid(T));
    currentScene = scenes[index];
    return static_pointer_cast<T>(scenes[index]);
}

template <typename T> std::shared_ptr<T> SceneManager::AddScene() {
    T scene = new T();    
    scenes[std::type_index(typeid(*scene))] = scene;
    return scene;
}

如果我在 game.cpp 文件中删除我的 AddScene 和 ChangeScene 方法调用,一切都会正确编译,并且 SceneManager 中定义的更新、初始化和渲染方法可以完美运行。我一直在研究这个问题,并浏览了MSDN LNK2019 Error中描述的所有问题

【问题讨论】:

    标签: c++ lnk2019


    【解决方案1】:

    模板函数必须定义在头文件中,而不是 cpp。

    Why can templates only be implemented in the header file?

    【讨论】:

    • 感谢您解决了我的问题。我专注于#includes 或其他东西。但是,因为我对 C++ 还很陌生,所以我试图找出最适合我的 map> 的方法。我想在作为组件类的子类的共享指针中调用对象的方法。我已经查看了很多答案,并希望确保我正确地处理它。最好做 ptr.get() 然后以某种方式将其转换为子类型,以便我可以调用特定的方法.. 或者有没有办法可以将我的地图初始化为 map>跨度>
    • @TaylorRiccetti 你能问一个单独的问题吗?我很难理解这个想法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-14
    • 2023-01-13
    • 1970-01-01
    • 2014-07-28
    • 1970-01-01
    相关资源
    最近更新 更多