enable_shared_from_this使用未包含memory头文件报错
class ISunny : public std::enable_shared_from_this<ISunny> {
public:
virtual ~ISunny() {};
virtual bool Initailize() = 0;
virtual bool Uninitailize() = 0;
};
class ISunnyService : public ISunny {
public:
virtual ~ISunnyService() = default;
};
class SunnyService : public ISunnyService {
public:
virtual bool Initailize() {}
virtual bool Uninitailize() {}
};
如果就上面的代码编译会报:
TestSunny.h:4:51: error: expected template-name before ‘<’ token
4 | class ISunny : public std::enable_shared_from_this<ISunny> {
| ^
TestSunny.h:4:51: error: expected ‘{’ before ‘<’ token
TestSunny.h:4:51: error: expected unqualified-id before ‘<’ token
TestSunny.h:12:30: error: invalid use of incomplete type ‘class ISunny’
12 | class ISunnyService : public ISunny {
| ^~~~~~
TestSunny.h:4:7: note: forward declaration of ‘class ISunny’
4 | class ISunny : public std::enable_shared_from_this<ISunny> {
原因:没有包含头文件 #include <memory>
版权声明:本文为jike28原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。