LNK2019 在.cpp文件中调用静态成员的方法时出错

LNK2019 Error when calling methods of a static member in .cpp file

我在我的代码中遇到这些错误,它们都与我游戏中的一个静态成员有关 class。

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 的子class 我的 SceneManager 是定义 AddScene 和 ChangeScene 方法的地方

我的SceneManager.h: #pragma once

#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

中描述的所有问题

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

Why can templates only be implemented in the header file?