这里是初学者。
我最近写了一些代码来使用spdlog进行日志记录。
我把它建立在一个";Singleton";基类,它似乎不起作用,这让我很恼火,因为在所有其他情况下,我都在使用这个确切的";Singleton";基类是有效的。
我得到以下错误:
1>D:devMakeshiftMakeshiftEnginesrcUtilityLog.h(20,33): error C2504: 'Singleton': base class undefined
1>D:devMakeshiftMakeshiftEnginesrcUtilityLog.h(20,33): error C2143: syntax error: missing ',' before '<'
1>D:devMakeshiftMakeshiftEnginesrcUtilityLog.h(24,16): error C3668: 'MS::Debug::Logger::Init': method with override specifier 'override' did not override any base class methods
需要处理的部分资源
辛格尔顿类
namespace MS
{
template <class T>
class Singleton
{
public:
static T& Get();
virtual void Init() {};
virtual void Shutdown() {};
protected:
explicit Singleton<T>() = default;
};
template<typename T>
T& Singleton<T>::Get()
{
static_assert(std::is_default_constructible<T>::value, "<T> needs to be default constructible");
static T m_Instance;
return m_Instance;
}
} // namespace MS
日志记录类
#include "Utility/Singleton.h"
namespace MS
{
namespace Debug
{
class Logger : public Singleton<Logger>
{
public:
virtual void Init() override;
static std::shared_ptr<spdlog::logger> getConsole();
protected:
static std::shared_ptr<spdlog::logger> m_Console;
};
}
}
和(如有必要(从调用的函数
MS::Debug::Logger::Get().Init();
// For those who are downvoting, could you please explain why?
// Is it my formatting?
// or Is my question just THAT dumb?
// If it is the latter, I am sorry, but I would still greatly appreciate it if you could answer it.
非常感谢你们提前回答:D
很抱歉,如果这是一个愚蠢的简单问题,我是一个初学者,我找不到答案,尽管它可能正盯着我的脸:(
[由"Igor Tandetnik"one_answers"drescherjm"给出的答案]
圆形包含!
Singleton.h
包含Log.h
,导致循环包含,导致编译器失控。
请始终检查您包含的内容,如果您包含的另一个文件包含您包含的文件,也可能发生这种情况。
我的pch
类也发生过这种情况
这就是为什么不应该在标题中包含pch
的原因。