在单例类中使用智能指针时出现C2248错误
在下面的代码中我遇到了 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
make_shared()
调用需要访问GameSetting
类的默认构造函数;但是,该构造函数被隐式声明为private
(这是类成员的默认访问说明符)。要解决此问题,只需在构造函数定义之前添加
public:
访问说明符即可:或者,如果您希望/需要将
GameSetting
构造函数保持私有(与单例中常见的情况相同)类),那么您可以将对std::make_shared
的调用替换为对std::shared_ptr
构造函数的直接“调用”(以及结果的赋值)。因此,您的 getInstance 函数将是:The
make_shared<GameSetting>()
call requires access to the default constructor of yourGameSetting
class; however, that constructor is implicitly declared asprivate
(which is the default access specifier for class members).To fix this issue, just add the
public:
access specifier before your constructor definition:Alternatively, if you wish/need to keep your
GameSetting
constructor private (as is common with singleton classes), then you can replace the call tostd::make_shared
with a direct 'call' to thestd::shared_ptr
constructor (and an assignment of the result). Thus, yourgetInstance
function would then be: