3

I've just started programming with SDL in C++ with Lazy Foo tutorials and I'm not really experienced in objective programming. I've started getting the LNK2019 Error and can't find the reason for it.

I have a class LTexture:

#pragma once
#include<string>
#include<SDL.h>
#include<SDL_image.h>
class LTexture
{
    public:
        LTexture();
        ~LTexture();
        //Loads image at specified path
        bool loadFromFile(std::string path, SDL_Renderer* _gameRenderer);
        void free();
        //Renders texture at given point
        void render(int x, int y, SDL_Renderer* _gameRenderer);
        int getWidth();
        int getHeight();
    private:
        //The actual hardware texture
        SDL_Texture* _texture;
        //Image dimensions
        int _width;
        int _height;
};

And a class AssetLoader:

#pragma once

#include <stdio.h>
#include<iostream>
#include "LTexture.hpp"

class AssetLoader
{
    public:
        //Loads media
        bool loadMedia(SDL_Renderer* _gameRenderer);        
        //Loads individual image as texture
        SDL_Texture* loadTexture(std::string path, SDL_Renderer* _gameRenderer);
        //Scene textures
        LTexture gFooTexture;
        LTexture gBackgroundTexture;

};

The errors I've been getting: Severity Code Description Project File Line Suppression State Error LNK2019 unresolved external symbol "public: bool __thiscall LTexture::loadFromFile(class std::basic_string,class std::allocator >,struct SDL_Renderer *)" (?loadFromFile@LTexture@@QAE_NV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@PAUSDL_Renderer@@@Z) referenced in function "public: bool __thiscall AssetLoader::loadMedia(struct SDL_Renderer *)" (?loadMedia@AssetLoader@@QAE_NPAUSDL_Renderer@@@Z)

Severity Code Description Project File Line Suppression State Error LNK2019 unresolved external symbol "public: __thiscall LTexture::LTexture(void)" (??0LTexture@@QAE@XZ) referenced in function "public: __thiscall AssetLoader::AssetLoader(void)" (??0AssetLoader@@QAE@XZ)

Severity Code Description Project File Line Suppression State Error LNK2019 unresolved external symbol "public: __thiscall LTexture::~LTexture(void)" (??1LTexture@@QAE@XZ) referenced in function __unwindfunclet$??0AssetLoader@@QAE@XZ$0

It apparently fails when it tries to call LTexture::loadFromFile function in AssetLoader::loadMedia function that looks like this:

#pragma once

#include"AssetLoader.hpp"
#include"LTexture.hpp"
bool AssetLoader::loadMedia(SDL_Renderer* _gameRenderer)
{
    //Loading success flag
    bool _success = true;

    //Load splash image
    if (!gFooTexture.loadFromFile("foo.png", _gameRenderer))
    {
        printf("Failed to load Foo texture image!\n");
        _success = false;
    }

    //Load background texture
    if (!gBackgroundTexture.loadFromFile("background.png", _gameRenderer))
    {
        printf("Failed to load background texture image!\n");
        _success = false;
    }

    return _success;
}

And the implementation of LTexture::loadFromFile is this:

#pragma once
#include"LTexture.hpp"
bool LTexture::loadFromFile(std::string path, SDL_Renderer* _gameRenderer)
{
    //Get rid of preexisting texture
    free();

    //The final texture
    SDL_Texture* _newTexture = NULL;

    //Load image at specified path
    SDL_Surface* _loadedSurface = IMG_Load(path.c_str());
    if (_loadedSurface == NULL)
    {
        printf("Unable to load image %s! SDL_image Error: %s\n", path.c_str(), IMG_GetError());
    }
    else
    {
        //Color key image
        SDL_SetColorKey(_loadedSurface, SDL_TRUE, SDL_MapRGB(_loadedSurface->format, 0, 0xFF, 0xFF));
        //Create texture from surface pixels
        _newTexture = SDL_CreateTextureFromSurface(_gameRenderer, _loadedSurface);
        if (_newTexture == NULL)
        {
            printf("Unable to create texture from %s! SDL Error: %s\n", path.c_str(), SDL_GetError());
        }
        else
        {
            //Get image dimensions
            _width = _loadedSurface->w;
            _height = _loadedSurface->h;
        }

        //Get rid of old loaded surface
        SDL_FreeSurface(_loadedSurface);
    }
    //Return success
    _texture = _newTexture;
    return _texture != NULL;
}

Please help, I really have nowhere else to ask.

  • 1
    Did you properly link all the C++ files? – Arnav Borborah Oct 05 '17 at 21:32
  • it compiled normally yesterday, i just added a couple of lines of code – Black Flicka Oct 05 '17 at 22:50
  • Make a clean build. Make sure your `LTexture` implementation file is compiled and resulting object file is linked into executable. Where is your `LTexture` constructor and destructor? (not present in question code, both reported as not-found, along with `loadFromFile`). Why do you have `#pragma once` in implementation file? (shouldn't be the source of your problem, but makes no sense either). – keltar Oct 06 '17 at 04:51
  • My guess would be, that you have no implementation of constructor/destructor of LTexture. Take a look at https://stackoverflow.com/a/12574403/5769463 – ead Oct 06 '17 at 04:54
  • i did implement it: `#pragma once #include"LTexture.hpp" LTexture::LTexture() { //Initialize _texture = NULL; _width = 0; _height = 0; } LTexture::~LTexture() { //Deallocate free(); }` #pragma once fixed my linker errors before so that's why it's there – Black Flicka Oct 06 '17 at 17:37

0 Answers0