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

时间:2018-09-02 19:55:24

标签: c++ lnk2019

我在代码中遇到这些错误,它们都与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)

在我的游戏中。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; 
};

在init()方法中我的game.cpp文件中发生了错误,而我的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

中描述的所有问题

1 个答案:

答案 0 :(得分:1)

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

Why can templates only be implemented in the header file?