将类更改为静态类的奇怪之处

Sla*_*ref 2 c++ static map sfml

所以我已经决定将我的类(资源管理器类)更改为c ++中的静态类,我收到了一条我无法理解的错误消息...也许你们可以帮助我?:)

继承人头文件:

#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
Run Code Online (Sandbox Code Playgroud)

这是我得到的错误:

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 ==========
Run Code Online (Sandbox Code Playgroud)

请温柔的家伙我是stackoverflow的新手:D

Mik*_*our 5

您已声明静态成员,但未定义它; 你需要一个(只有一个)源文件中的定义:

#include "ResourceManager.h"

sz::TStrImageMap sz::ResourceManager::images;
Run Code Online (Sandbox Code Playgroud)

如果您不确定声明和定义之间的区别,这里是一个彻底的讨论.