gpt4 book ai didi

c++ - 尝试在C++中编译单例时出现编译错误

转载 作者:行者123 更新时间:2023-12-02 11:04:19 26 4
gpt4 key购买 nike

我正在尝试学习c++。
我写了一个文件“Singleton.h”,如下所示:

class Singleton
{
private:
static Singleton* m_this;
Singleton();
public:
static Singleton* getInstance(){
return m_this;
}
virtual ~Singleton();
};

我的Singleton.cpp文件:
#include "StdAfx.h"
#include "Singleton.h"
Singleton::Singleton(){}
Singleton::~Singleton(){}

我在main方法中调用getInstance方法,如下所示:
Singleton* s = NULL;
s = Singleton.getInstance();

但是,我得到了编译错误:
error C2275: 'Singleton' : illegal use of this type as an expression

你知道那是为什么吗?

最佳答案

s = Singleton::getInstance();

不是 .,而是静态方法的 ::(范围分辨率运算符)。

关于c++ - 尝试在C++中编译单例时出现编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22895715/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com