简体   繁体   English

C ++不命名为类型

[英]C++ Does not name to a type

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. 当我尝试在我的环境类中添加类型为Agent的对象时,我得到的Agent没有命名为类型错误。 I am including Agent.h in my Environment.h class 我在我的Environment.h类中包含Agent.h

#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

Your agent.h includes environment.h. 您的agent.h包括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. agent.h文件是按从上到下的顺序进行解析的,因此,在解析environment.h时,编译器不知道什么是Agent。 There appears to be no reason to incude environment.h in agent.h. 似乎没有理由在agent.h中包含environment.h。

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). 没有任何理由让Agent.h包含Environment.h,因此,如果.cpp文件首先包含Agent.h,它将失败(因为它将首先通过需要Agent的Environment.h)。

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. 如果您遇到两个头文件依赖于彼此的定义的情况,请在可能的地方使用前向声明,或将头文件拆分为更多的头文件。

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

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