尚未宣布枚举
Enum has not been declared?
我会遇到此错误。就像编译器无法识别我的声明
g++ -c main.cc
In file included from Storage.h:7:0,
from Server.h:5,
from Control.h:8,
from main.cc:5:
Serializer.h:11:36: error: ‘Storage::UpdateType’ has not been declared
Serializer.h:12:45: error: ‘Storage::UpdateType’ has not been declared
make: *** [main.o] Error 1
任何人都知道这个错误是什么,因为枚举已经声明了。受影响的代码如下:
serializer.h
#ifndef SERIALIZER_H
#define SERIALIZER_H
#include "Storage.h"
class Storage;
class Serializer{
public:
Serializer();
void serialize(List&, Storage::UpdateType&, std::string&);
void deserialize(std::string&, Storage::UpdateType&, List&);
};
#endif
存储.h
#ifndef STORAGE_H
#define STORAGE_H
#include "List.h"
#include "Interface.h"
#include "Movie.h"
#include "Serializer.h"
class Storage{
public:
enum UpdateType {ADD, DELETE, RETRIEVE};
Storage();
~Storage();
List* list;
void retrieve(List*);
void update(UpdateType, List*);
void handleRequest(string&, string&);
private:
//Serializer serial;
};
#endif
您遇到的问题是您的代码被解释如下:
#ifndef STORAGE_H
#define STORAGE_H
// replacing the include with the Serializer.h file
#include "List.h"
#include "Interface.h"
#include "Movie.h"
// replacing the include with the Storage.h file
#ifndef STORAGE_H // returns true
#endif
class Storage;
class Serializer{
public:
Serializer();
// here Storage::UpdateType is still unknown to the compiler
void serialize(List&, Storage::UpdateType&, std::string&);
void deserialize(std::string&, Storage::UpdateType&, List&);
};
#endif
class Storage{
public:
// as it gets declared here
enum UpdateType {ADD, DELETE, RETRIEVE};
Storage();
~Storage();
List* list;
void retrieve(List*);
void update(UpdateType, List*);
void handleRequest(string&, string&);
private:
//Serializer serial;
};
#endif
最好的解决方案是,恕我直言,是从存储类中提取enum UpdateType
,然后在Serialializer中向前声明。甚至在序列化器标头中声明它,因为Storage
正在封装Serializer
的内容,因此基本上,UpdateType
的内容应与Serializer
材料相同的封装上下文。
另一个解决方案是@nim建议,包括序列化器中的存储,向前声明序列化器在存储中,并将序列化器包括在main中。但这可能会欺骗您认为设计的方式,扭转了Serializer
和Storage
的封装。
最后,我确实不确定这是否可能以及语法如何,但是如果是,则可以简单地将serialializer中的 enum Storage::UpdateType
转发。我想这就是您希望的。
相关文章:
- 不带大括号的枚举形式
- 枚举环境变量的惯用C++14/C++17方法
- 类似枚举的计算常量
- 如何正确实现和访问运算符的各种自定义枚举器
- 错误:从"int"到枚举c++的转换无效
- C++中构造函数中的枚举
- typedef 枚举和枚举类有什么区别?
- 无法将返回的枚举与枚举进行比较
- 带有constexpr std :: string_view vs用std :: string实例化枚举的枚举
- Qt 排队插槽中的枚举和枚举类
- 调用模板化类内的枚举类枚举器的正确语法是什么
- 如何将C 枚举类枚举作为std ::阵列索引而没有明确的铸件
- 无作用域枚举、枚举器和基础类型C++中的歧义
- 函数采用枚举或枚举值组合的模式
- C++在类中声明静态枚举与枚举
- 枚举与枚举类的区别
- 枚举与枚举类的区别
- 是否可以别名枚举-类枚举器
- 在c++中是否可以有枚举的枚举?
- 无法访问嵌套私有枚举的枚举