简体   繁体   English

枚举尚未声明?

[英]Enum has not been declared?

I'm getting this error. 我收到此错误。 It's like the compiler does not recognise my declarations 就像编译器无法识别我的声明

 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

Anyone have an idea what this error is about because the enum has already beeen declared. 任何人都知道此错误是什么原因,因为已经声明了该枚举。 The affected code is below: 受影响的代码如下:

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

Storage.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

The problem you have is that your code is interpreted as follows: 您遇到的问题是您的代码解释如下:

#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

the best solution, imho, would be to extract the enum UpdateType from your storage class and forward declare it in Serializer. 最好的解决方案,恕我直言,将是从您的存储类中提取enum UpdateType并在Serializer中对其进行声明。 Or even declare it within the Serializer header, as Storage is encapsulating Serializer stuff, so basically, the UpdateType stuff should be in the same encapsulated context as the Serializer stuff. 甚至在Serializer标头中声明它,因为Storage正在封装Serializer东西,因此,基本上, UpdateType东西应该与Serializer东西处于相同的封装上下文中。

Another solution would be @Nim suggests, to include Storage from Serializer, forward declare Serializer in Storage and include Serializer in main. @Nim建议另一种解决方案,包括从Serializer中存储,在Storage中向前声明Serializer,在main中包含Serializer。 But it may deceives the way you have thought your design out, reversing the encapsulation of Serializer and Storage . 但这可能会欺骗您思考设计的方式,从而颠倒了SerializerStorage的封装。

Finally, I'm really not sure if this is possible and how would be the syntax, but if it is, you could simply forward declare enum Storage::UpdateType in Serializer. 最后,我真的不确定这是否可行以及语法如何,但是如果可以,您可以简单地在Serializer中转发声明enum Storage::UpdateType And I guess that's what you wish would be possible. 我想这就是您所希望的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM