C++不命名类型

发布于 2024-09-26 00:14:11 字数 2398 浏览 2 评论 0原文

这可能是一个简单的问题,但我无法弄清楚为什么编译器会给我这个错误。我有两节课。代理和环境。当我尝试在环境类中添加 Agent 类型的对象时,出现 Agent does not name to type 错误。我将 Agent.h 包含在我的 Environment.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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

七堇年 2024-10-03 00:14:11

您的agent.h 包括environment.h。 agent.h文件是按照从上到下的顺序解析的,所以当解析environment.h时,编译器并不知道Agent是什么。似乎没有理由在agent.h 中包含environment.h。

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.

忘你却要生生世世 2024-10-03 00:14:11

除了已经说过的注释之外,您不能让两个头文件相互包含。 Agent.h 没有理由包含Environment.h,因此如果.cpp 文件首先包含Agent.h,它将失败(因为它将首先经过Environment.h,这需要Agent)。

如果遇到两个头文件相互依赖的情况,请尽可能使用前向声明,或者将头文件拆分为更多头文件。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文