0

I have the usual unresolved external symbol problem

LNK2019 unresolved external symbol "public: void __thiscall 
Aldo::AssetManager::LoadTexture(class std::basic_string<char,struct std::char_traits<char>,
class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" 
(?LoadTexture@AssetManager@Aldo@@QAEXV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@0@Z) 
referenced in function "public: virtual void __thiscall Aldo::SplashState::Init(void)"
 (?Init@SplashState@Aldo@@UAEXXZ)   
SFML template   C:\Users\ajb266\Documents\Aldo\SFML template\SFML template\SplashState.obj  1

It seems the issue is LoadTexture is not defined, but then I have defined the LoadTexture constructor in AssetManager's cpp file. It seems SplashState could also be involved. From what I understand, the object file SplashState is created after compilation of SplashState.cpp, and then linked with the libraries using a linker. Maybe I am missing something with the linker settings in Visual Studio?

My relevant files are as follows:

AssetManager.hpp:

#pragma once

#include <map>

#include <SFML/Graphics.hpp>

namespace Aldo
{
    class AssetManager {
    public:
        AssetManager() {}
        ~AssetManager() {}
        void LoadTexture(std::string name, std::string fileName);
        sf::Texture &GetTexture(std::string name);

        void LoadFont(std::string name, std::string fileName);
        sf::Font &GetFont(std::string name);

    private: 
        std::map<std::string, sf::Texture> _textures;
        std::map<std::string, sf::Font> _fonts;
    };
}

AssetManager.cpp:

#include "AssetManager.hpp"
namespace Aldo {
    void AssetManager::LoadTexture(std::string name, std::
        string fileName) {
        sf::Texture tex;
        if (tex.loadFromFile(fileName)) {
            this->_textures[name] = tex;
        }
    }
    sf::Texture& AssetManager::GetTexture(std::string name) {
        return this->_textures.at(name);
    }

    void AssetManager::LoadFont(std::string name, std::string fileName) {
        sf::Font font;
        if (font.loadFromFile(fileName)) {
            this->_fonts[name] = font;
        }
    }
    sf::Font& AssetManager::GetFont(std::string name) {
        return this->_fonts.at(name);
    }
}

SplashState.hpp:

#pragma once

#include <SFML/Graphics.hpp>
#include "state.hpp" 
#include "GameLoop.hpp"

namespace Aldo {
    class SplashState : public State
    {
    public:
        SplashState(GameDataRef data);

        void Init();
        void HandleInput();
        void Update(float dt);
        void Draw(float dt);

    private:
        GameDataRef _data;
        sf::Clock _clock;

        sf::Texture _backgroundTexture;
        sf::Sprite _background;
    };
}

SplashState.cpp:

#include <sstream>
#include "SplashState.hpp"
#include "DEFINITIONS.hpp"

#include <iostream>

namespace Aldo
{
    SplashState::SplashState(GameDataRef data) : _data(data) 
    {

    }
    void SplashState::Init() {
        _data->assets.LoadTexture("Splash State Background",
            SPLASH_SCENE_BACKGROUND_FILEPATH);
        _background.setTexture(this->_data->assets.GetTexture("Splash State Background"));
    }
    void SplashState::HandleInput() {/*only handle and check whether the X button to close the window is pressed or not*/
        sf::Event event;

        while (_data->window.pollEvent(event))
        {
            if (sf::Event::Closed == event.type) {
                _data->window.close();
            }
        }
    }
    void SplashState::Update(float dt) {
        if (_clock.getElapsedTime().asSeconds() > SPLASH_STATE_SHOW_TIME) {
            std::cout << "go to main menu" << std::endl;
        }
    }
    void SplashState::Draw(float dt) {
        _data->window.clear();
        _data->window.draw(_background);
        _data->window.display();
    }
}

I'm using Visual Studio and c++17. I'm new to C++ so forgive me if this is too trivial.

  • 3
    Silly Question: Look at your project source files in the project explorer of your solution in VS. `AssetManager.cpp` is in the list, *right* ?? – WhozCraig Mar 17 '21 at 17:20
  • `LoadTexture` is not a constructor, it's a regular member function. – molbdnilo Mar 17 '21 at 17:23
  • https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix?rq=1 – R Sahu Mar 17 '21 at 17:23
  • If you could provide more code, such as `state, GameLoop, DEFINITIONS`, it will help me to reproduce this problem. Actually, I didn't find any problems. Therefore, I suggest you check the following points: 1. copy `sfml-graphics-d.lib;sfml-window-d.lib;sfml-system-d.lib;sfml-audio-d.lib;` in `Properties->Linker->Input->Additional Dependencies`. 2. Copy all the dll files in the `bin` directory to `C:\Windows\System32 and C:\Windows\SysWOW64`. – Barrnet Chou Mar 18 '21 at 02:17

0 Answers0