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

问题描述 投票:0回答:1

我在我的代码中遇到这些错误,它们都与我的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中定义的更新,init和render方法运行完美。我一直在研究这个问题,并逐步解决了MSDN LNK2019 Error中描述的所有问题

c++ lnk2019
1个回答
1
投票

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

Why can templates only be implemented in the header file?

© www.soinside.com 2019 - 2024. All rights reserved.