在单例类中使用智能指针时出现C2248错误

发布于 2025-01-12 22:39:25 字数 1299 浏览 3 评论 0原文

在下面的代码中我遇到了 C2248 错误。当我用原始指针替换智能指针时,它会编译。不过我想使用智能指针 std::shared_ptr<>目的。 使用智能指针时如何纠正此错误?

    #include <iostream>
    #include <string>
    #include <memory>

    class GameSetting{
    inline static std::shared_ptr<GameSetting> _instance;
    unsigned int _brightness;
    unsigned int _width;
    unsigned int _heigth;
    GameSetting():_brightness{150}, _width{300}, _heigth{200}
   {}

   public:
   static std::shared_ptr<GameSetting> getInstance(){
    if(! _instance)
        _instance=std::make_shared<GameSetting>();
    return _instance;
   }


  void setBrightness(unsigned brightness){_brightness=brightness;}
  void setWidth(unsigned  int width){_width=width;}
  void setHeight(unsigned int height){_heigth=height;}

  unsigned int getBrightness()const{return  _brightness;}
  unsigned int getWidth()const{return _width;}
  unsigned int getHeigth()const{return _heigth;}

  void displaySettings()const{
    std::cout<<"brigthness: "<<_brightness<<'\n'<<"width: "<<_width
            <<'\n'<<"heigth: "<<_heigth<<"\n\n";
  }
  };



  int main()
  {
  std::shared_ptr<GameSetting> setting=GameSetting::getInstance();
  setting->displaySettings();

  }

In below code I'm having C2248 error. When I replace smart pointers with the raw pointers it compiles. However I want to use smart pointer std::shared_ptr<> object.
How can I correct this error when using smart pointer?

    #include <iostream>
    #include <string>
    #include <memory>

    class GameSetting{
    inline static std::shared_ptr<GameSetting> _instance;
    unsigned int _brightness;
    unsigned int _width;
    unsigned int _heigth;
    GameSetting():_brightness{150}, _width{300}, _heigth{200}
   {}

   public:
   static std::shared_ptr<GameSetting> getInstance(){
    if(! _instance)
        _instance=std::make_shared<GameSetting>();
    return _instance;
   }


  void setBrightness(unsigned brightness){_brightness=brightness;}
  void setWidth(unsigned  int width){_width=width;}
  void setHeight(unsigned int height){_heigth=height;}

  unsigned int getBrightness()const{return  _brightness;}
  unsigned int getWidth()const{return _width;}
  unsigned int getHeigth()const{return _heigth;}

  void displaySettings()const{
    std::cout<<"brigthness: "<<_brightness<<'\n'<<"width: "<<_width
            <<'\n'<<"heigth: "<<_heigth<<"\n\n";
  }
  };



  int main()
  {
  std::shared_ptr<GameSetting> setting=GameSetting::getInstance();
  setting->displaySettings();

  }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

〆一缕阳光ご 2025-01-19 22:39:26

make_shared() 调用需要访问 GameSetting 类的默认构造函数;但是,该构造函数被隐式声明为private(这是类成员的默认访问说明符)。

要解决此问题,只需在构造函数定义之前添加 public: 访问说明符即可:

class GameSetting {
    inline static std::shared_ptr<GameSetting> _instance;
    unsigned int _brightness;
    unsigned int _width;
    unsigned int _heigth;
public: // Anything before this is "private" by default
    GameSetting() :_brightness{ 150 }, _width{ 300 }, _heigth{ 200 }
    {}
public:
    static std::shared_ptr<GameSetting> getInstance()
    {
        if (!_instance)
            _instance = std::make_shared<GameSetting>();
        return _instance;
    }
   // ... the rest of you class definition

或者,如果您希望/需要将 GameSetting 构造函数保持私有(与单例中常见的情况相同)类),那么您可以将对 std::make_shared 的调用替换为对 std::shared_ptr 构造函数的直接“调用”(以及结果的赋值)。因此,您的 getInstance 函数将是:

    static std::shared_ptr<GameSetting> getInstance()
    {
        if (!_instance)
            _instance = std::shared_ptr<GameSetting>{new GameSetting};
        return _instance;
    }

The make_shared<GameSetting>() call requires access to the default constructor of your GameSetting class; however, that constructor is implicitly declared as private (which is the default access specifier for class members).

To fix this issue, just add the public: access specifier before your constructor definition:

class GameSetting {
    inline static std::shared_ptr<GameSetting> _instance;
    unsigned int _brightness;
    unsigned int _width;
    unsigned int _heigth;
public: // Anything before this is "private" by default
    GameSetting() :_brightness{ 150 }, _width{ 300 }, _heigth{ 200 }
    {}
public:
    static std::shared_ptr<GameSetting> getInstance()
    {
        if (!_instance)
            _instance = std::make_shared<GameSetting>();
        return _instance;
    }
   // ... the rest of you class definition

Alternatively, if you wish/need to keep your GameSetting constructor private (as is common with singleton classes), then you can replace the call to std::make_shared with a direct 'call' to the std::shared_ptr constructor (and an assignment of the result). Thus, your getInstance function would then be:

    static std::shared_ptr<GameSetting> getInstance()
    {
        if (!_instance)
            _instance = std::shared_ptr<GameSetting>{new GameSetting};
        return _instance;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文