【问题标题】:something weird with changing a class to a static one将类更改为静态类有点奇怪
【发布时间】:2012-02-09 11:33:30
【问题描述】:

所以我决定将我的类(资源管理器类)更改为 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

这是我得到的错误:

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 ==========

请对我刚接触stackoverflow的人温柔一点:D

【问题讨论】:

标签: c++ map static sfml


【解决方案1】:

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

#include "ResourceManager.h"

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

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

【讨论】:

    【解决方案2】:

    在类内部声明静态成员变量时,还需要在类外部定义它。这可以确保变量在堆上分配空间。

    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.
    
    }
    

    【讨论】:

    • 几乎正确;但定义必须放在源文件中,而不是头文件中,以便在一个翻译单元中定义。
    • 所以我尝试了这个但仍然收到相同的错误消息:S
    • @MikeSeymour:我同意!我的错。我错过了这是在头文件中定义的。它应该在源文件中定义。谢谢!
    【解决方案3】:

    我认为问题在于您在任何地方都没有 ResourceManager::images 的实例。由于它是静态的,因此需要在源代码中的某个位置进行定义。

    【讨论】:

      【解决方案4】:

      在声明静态成员变量时,您应该初始化它们以确保即使您的类不存在任何实例也有可用的值。举个简短​​的例子:

      class MyClass {
      private:
          static int value_;
      };
      
      int MyClass::value_ = 0;
      

      在很多情况下,调用默认构造函数就足够了。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-03-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-17
        • 1970-01-01
        • 1970-01-01
        • 2014-05-20
        相关资源
        最近更新 更多