0

When trying to compile my program this error shows up:

Error   1   error LNK2001: unresolved external symbol "public: static class sf::Texture TextureManager::texture" (?texture@TextureManager@@2VTexture@sf@@A) 

This is my code:

main.cpp:

int main()
{
     TextureManager::Initialize();
}

TextureManager.h:

#include <SFML\Graphics.hpp>
using namespace sf;

class TextureManager
{
    public:
        static Texture texture;
    public:
        static void Initialize();
};

TextureManager.cpp:

#include <SFML\Graphics.hpp>
#include <iostream>
#include "TextureManager.h"

using namespace sf;

void TextureManager::Initialize()
{
    if(!texture.loadFromFile("Textures\\Blocks\\Texture.png"))
    {
        std::cout << "Error!";
    }
    else
    {
        std::cout << "Sucess!";
    }
}

I've tried searching for any solutions (including this site) but have not found any.

  • 1
    Please search this site for your error before posting. In this case, search for `[c++] unresolved external symbol`, and you'll find literally hundreds of hits. If you didn't find them, you didn't search for the error message. – Ken White Aug 16 '13 at 16:09

1 Answers1

6

When you have a static member in C++, you should define it in your .cpp :

static Texture Texture::texture;

This is because static members must be defined in exactly one translation unit, in order to not violate the One-Definition Rule.


You can do it at the top of your TextureManager.cpp:

#include <SFML\Graphics.hpp>
#include <iostream>
#include "TextureManager.h"

using namespace sf;

static Texture Texture::texture; // <-

void TextureManager::Initialize()
{
}
Pierre Fourgeaud
  • 14,290
  • 1
  • 38
  • 62