2

so ive descided to change my class ( a resource manager class ) into a static class in c++ and i got an error message which i can not understand... maybe you guys can help me? :)

heres the header file:

#ifndef RESOURCEMANAGER_H
#define RESOURCEMANAGER_H

#include <iostream>
#include <map>
#include <string>
#include "Image.h"

namespace sz {
    typedef std::map<std::string, sz::Image> TStrImageMap;
    typedef std::pair<std::string, sz::Image> TStrImagePair;

    class ResourceManager {
    private:
        static TStrImageMap images; // Name, Image(sz::Image)
    public:

        static void AddImage(std::string name, std::string path);
        //void AddSound();

        static sz::Image &GetImage(std::string name);
        //GetSound();
    };
}
#endif

and here is the error that im getting:

1>------ Build started: Project: Basic SFML, Configuration: Debug Win32 ------
1>LINK : warning LNK4098: defaultlib 'LIBCMT' conflicts with use of other libs; use /NODEFAULTLIB:library
1>ResourceManager.obj : error LNK2001: unresolved external symbol "private: static class std::map<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class sz::Image,struct std::less<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >,class std::allocator<struct std::pair<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const ,class sz::Image> > > sz::ResourceManager::images" (?images@ResourceManager@sz@@0V?$map@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@VImage@sz@@U?$less@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@V?$allocator@U?$pair@$$CBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@VImage@sz@@@std@@@2@@std@@A)
1>C:\Users\Gannash\Desktop\Game Project\Debug\Basic SFML.exe : fatal error LNK1120: 1 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

please be gentle guys im new to stackoverflow :D

Slava Zoref
  • 155
  • 3
  • 16
  • 2
    In C++, you must define your static class members so they get allocated; see also the [C++ FAQ on this topic](http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.11). – Alex P. Feb 09 '12 at 11:38

4 Answers4

5

You have declared the static member, but not defined it; you need a definition in one (and only one) source file:

#include "ResourceManager.h"

sz::TStrImageMap sz::ResourceManager::images;

If you're not sure about the difference between a declaration and a definition, here is a thorough discussion.

Community
  • 1
  • 1
Mike Seymour
  • 235,407
  • 25
  • 414
  • 617
2

When a static member variable is declared inside a class, you also need to define it outside the class. This makes sure that the variable is allocated space on the heap.

namespace sz {
typedef std::map<std::string, sz::Image> TStrImageMap;
typedef std::pair<std::string, sz::Image> TStrImagePair;

class ResourceManager {
private:
    static TStrImageMap images; // Name, Image(sz::Image)
public:

    static void AddImage(std::string name, std::string path);
    //void AddSound();

    static sz::Image &GetImage(std::string name);
    //GetSound();
};

TStrImageMap ResourceManager::images; //add this line to define the static variable.

}
Chethan Ravindranath
  • 1,831
  • 1
  • 15
  • 27
1

I think the issue is that you don't have anywhere an instance of ResourceManager::images. As it is static, it needs to be defined in your source code somewhere.

Tom Tanner
  • 8,967
  • 2
  • 28
  • 58
0

When declaring static member variables, you are expected to initialize them to be sure to have a value available even if no instance of your class exists. In a short example:

class MyClass {
private:
    static int value_;
};

int MyClass::value_ = 0;

In many cases, calling the default constructor will suffice.

nijansen
  • 8,041
  • 8
  • 47
  • 68