2014-03-07 154 views
0

我收到此錯誤。這就像編譯器無法識別我聲明Enum沒有被聲明?

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 

任何人都有一個想法是什麼這個誤差約爲因爲枚舉已經beeen聲明。受影響的代碼如下:

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 
+0

循環包含依賴項:'Storage.h'包含'Serializer.h',反之亦然。這是行不通的。 – juanchopanza

+0

不,它沒有,因爲它具有防止循環依賴的宏觀防護。 – zmo

+0

@zmo是的。衛兵不解決這個問題。 – juanchopanza

回答

0

你的問題是,如下代碼解釋:

#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並轉發decl它在串行器中。或者甚至在Serializer頭文件中聲明它,因爲Storage正在封裝Serializer的東西,所以基本上,UpdateType的東西應該與Serializer東西處於相同的封裝上下文中。

另一種解決方案是@Nim建議,包括Storage from Serializer,在存儲中轉發聲明Serializer,並在主要中包含Serializer。但它可能會欺騙你認爲你的設計出來的方式,扭轉了SerializerStorage的封裝。

最後,我真的知道這是可能的,怎麼會是語法,但如果是這樣,你可以簡單地向前串行聲明enum Storage::UpdateType。我想這就是你想要的。