views:

80

answers:

2

This might be an easy question, but I cannot figure out why the compiler it's giving me this error. I have two classes. Agent and Environment. WHen I try to add an object of type Agent in my Environment class I get Agent does not name to a type error. I am including Agent.h in my Environment.h class

#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
+1  A: 

Apart from what the comments already said, you can't have two header files include each other. There is no reason for Agent.h to include Environment.h, so if a .cpp file includes Agent.h first, it'll fail (since it will first go through Environment.h, which requires Agent).

IF you have a situation where two header files depend on each other's definitions, use forward declarations where you can, or split your header files up into more header files.

EboMike
+3  A: 

Your agent.h includes environment.h. The agent.h file is parsed in order from top to bottom, so when environment.h is parsed, the compiler doesn't know what an Agent is. There appears to be no reason to incude environment.h in agent.h.

Andrew
I'd add a mention to forward declarations, to use when there *is* such a reason.
Matteo Italia