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

Why can I not #include my class from another file in the same directory?

本文关键字:另一个 文件 我的 #include 不能 为什么      更新时间:2023-10-16

我有三个文件,结构如下

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

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

事件.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

事件.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;
}

事件Ptr.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;

有人知道发生了什么吗?

可能你有一个循环包含依赖项。

请检查 事件.h 包含的文件。如果您发现包含 EventPtr.h,这可能是错误。

我给你留下一个维基百科链接:循环依赖

相关文章: