2010-10-02 72 views
0

這可能是一個簡單的問題,但我不明白爲什麼編譯器給了我這個錯誤。我有兩個班。代理和環境。當我試圖在我的環境類中添加一個Agent類型的對象時,我得到的代理程序沒有命名爲類型錯誤。我包括Agent.h我Environment.h類C++不命名爲類型

#ifndef AGENT_H_INCLUDED 
#define AGENT_H_INCLUDED 

#include <vector> 
#include <iostream> 
#include "Environment.h" 

using namespace std; 

class Agent{ 
    public: 
     Agent(bool s); 
     vector<int> getPercept(); 
     void setPercept(vector<int> p); 
     void goForward(); 
     void turnRight(); 
     void turnLeft(); 
     void clean(); 
     void paint(); 
     void refuel(); 
     bool needsRefuel(); 
     void turnOn(); 
     void turnOff(); 
     bool isActive(); 
     void move(); 
     int getCurX(); 
     int getCurY(); 
     char getCurDir(); 
     void setCurrentPosition(int x, int y, char d); 


    private: 
     vector<int> percept; 
     int actions; 
     int performance; 
     char direction; 
     bool isOn; 
     int curX; 
     int curY; 
     char curDir; 
}; 

#endif // AGENT_H_INCLUDED 

/* ** * ** * ** * ** * ** * * * * ** * ** * /

#ifndef ENVIRONMENT_H_INCLUDED 
#define ENVIRONMENT_H_INCLUDED 

#include <vector> 
#include <iostream> 
#include "Agent.h" 

using namespace std; 


class Environment{ 
    public: 

     Environment(vector<vector<char> > roomData); 
     Environment(vector<vector<char> > roomData, vector<int> status); 
     void setRoomData(vector<vector<char> > roomData); 
     bool isSimulationComplete(); 
     void isAgentHome(); 
     vector<int> sendLocationStatus(); 
     void printEnvironment(); 
     void setAgentHome(int x, int y); 
     vector<int> getAgentPercept(); 
     void setAgentPercept(vector<int> status); 
     void setAgentPosition(int x, int y, char p); 
     vector<int> sendAgentPercept(); 
     void calculateAgentPercept(); 


    private: 
     vector<vector<char> > room; 
     vector<int> agentPercept; 
     bool simulationComplete; 
     int agentHomeX; 
     int agentHomeY; 
     int agentX; 
     int agentY; 
     char agentDir; 
     Agent agent; ////ERROR IS HERE 
}; 

#endif // ENVIRONMENT_H_INCLUDED 
+3

什麼錯誤?你在頭文件之間有一個循環依賴關係。 – avakar 2010-10-02 15:21:15

+0

代理未命名爲類型 – user69514 2010-10-02 15:24:28

+5

您在標頭中使用名稱空間標準。 **不要這樣做。**這會將整個標準名稱空間導入到標題和所有文件'#include'中,除了本身不好之外 - 還可能導致奇怪的和看起來不相關的編譯錯誤,通常代碼完全不同的部分。 – 2010-10-02 15:32:18

回答

4

你agent.h包括environment.h。 agent.h文件按從上到下的順序進行分析,因此,當分析environment.h時,編譯器不知道代理是什麼。在agent.h中似乎沒有理由要包含environment.h。

+0

我會添加一個提到轉發聲明,使用時,*有*這樣的原因。 – 2010-10-02 16:13:08

1

除了那些已經評論說,你不能有兩個頭文件互相包含。 Agent.h沒有理由包含Environment.h,因此如果一個.cpp文件首先包含Agent.h,它將會失敗(因爲它首先會通過需要代理的Environment.h)。

如果你有其中兩個頭文件互相依賴的定義,使用前向聲明在那裏你可以,或最多拆分頭文件到多個頭文件的情況。