为什么不能#include同一目录中另一个文件中的类?

时间:2020-01-05 18:12:24

标签: c++ include

我具有以下结构的三个文件

- src/
  - events
    - ...
    - Event.cpp
    - Event.h
    - EventPtr.h
    - ...

问题是#include Event.h中的EventPtr.h似乎不起作用。这是代码:

Event.h


#ifndef POKERSIMULATIONSINCPP_EVENT_H
#define POKERSIMULATIONSINCPP_EVENT_H

#include <iostream>
#include "game/Table.h"
#include "players/Player.h"

namespace events {

    enum TargetType {
        Dealer, Table, None, Players
    };


    class Event {
    private:
        TargetType target = None;
        std::string description = "Base event class";
        bool done = false;
    public:
        ~Event();

        Event();

        TargetType getTarget();

        std::string getDescription();

        bool getDone();

    };


}

#endif //POKERSIMULATIONSINCPP_EVENT_H

Event.cpp


#include "Event.h"
#include <iostream>

namespace events {

    TargetType Event::getTarget() {
        return target;
    }

    std::string Event::getDescription() {
        return description;
    }

    bool Event::getDone() {
        return done;
    }

    Event::~Event() = default;

    Event::Event() = default;
}

EventPtr.h

#ifndef POKERSIMULATIONSINCPP_EVENTPTR_H
#define POKERSIMULATIONSINCPP_EVENTPTR_H

#include <memory>
#include "events/Event.h"

namespace events {

    typedef std::shared_ptr<Event> EventPtr;

}

#endif //POKERSIMULATIONSINCPP_EVENTPTR_H

哪个出现以下错误:

错误

D:/PokerSimulationsInCpp/src/events/EventPtr.h:13:29: error: 'Event' was not declared in this scope
     typedef std::shared_ptr<Event> EventPtr;

我也尝试过EventPtr.h

EventPtr.h,第二次尝试


#ifndef POKERSIMULATIONSINCPP_EVENTPTR_H
#define POKERSIMULATIONSINCPP_EVENTPTR_H

#include <memory>
#include "events/Event.h"
#include "Event.h"

namespace events {

    typedef std::shared_ptr<events::Event> EventPtr;

}

#endif //POKERSIMULATIONSINCPP_EVENTPTR_H

哪个出现以下错误:

D:/PokerSimulationsInCpp/src/events/EventPtr.h:14:37: error: 'Event' is not a member of 'events'
     typedef std::shared_ptr<events::Event> EventPtr;

有人知道怎么回事吗?

1 个答案:

答案 0 :(得分:1)

可能您有一个循环包含依赖关系。

请检查Event.h包含的文件。如果找到包含的EventPtr.h,则可能是错误。

我为此留了一个维基百科链接:Circular Dependency