Search code examples
c++include

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


I have three files in the following structure

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

The issue is that the #include Event.h inside EventPtr.h doesn't seem to be working. Here is the code:

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

Which gives the following error:

The Error

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

I've also tried this for EventPtr.h

EventPtr.h, second try


#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

Which gives the following error:

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

Anybody know what is going on?


Solution

  • Probably you have a circular include dependencies.

    Please Check the file included by Event.h . If you find EventPtr.h included this could be the error.

    I leave you a wikipedia link on this : Circular Dependency