简体   繁体   中英

Troubles with Circular Dependencies between 3 classes and with inheritance

I'm a first-year college student that doesn't know everything about CS yet, so please bear with my newness to it, and this is my first question on here.

For an assignment, we are making faux version of Pokemon Go to practice using polymorphism in c++, and I'm running into some compiler errors. Here are the three files with just a sample of the code in them:

#ifndef EVENT_H
#define EVENT_H

#include <string>
#include "Trainer.h"

class Event{
    protected:
        std::string title;
    public:
    Event();
    ~Event();

    virtual void action(Trainer) = 0;

};
#endif

Trainer.h:

#ifndef TRAINER_H
#define TRAINER_H
#include "Pokemon.h"
class Trainer{
    private:
        Pokemon* pokemon;
        int num_pokemon;
    public:
        Trainer();
        ~Trainer();
    //include accessors and mutators for private variables    
};
#endif

Pokemon.h:

#ifndef POKEMON_H
#define POKEMON_H
#include "Event.h"
#include <string>
class Pokemon : public Event{
    protected:
        std::string type;
        std::string name;
    public:
        Pokemon();
        ~Pokemon();
        virtual bool catch_pokemon() = 0;

};
#endif

The trainer.h file is a parent class for each pokemon type (eg Rock) which just defines a few virtual functions. The error I'm getting is when I'm compiling all of this and I get something that says:

Pokemon.h : 5:30: error: expected class-name befoer '{' token:
  class Pokemon : Event {

Pokemon need to be a derived class to an event, so that an event pointer can point in another Location class can point to either a pokemon, pokestop, or cave for the assignment, and I have been looking online for hours and can't figure out what to do. I would appreciate the help! Let me know if you need more info or something because again, this is my first time posting a question.

You need some forward declarations.

In Event.h, you can put class Trainer; instead of #include "Trainer.h" . In Trainer.h, you can put class Pokemon; instead of #include "Pokemon.h" .

You will probably need to include the appropriate headers in the corresponding source files in order to actually use the other classes. But by avoiding the includes in the header files, you get out of the circular dependency trouble.

Pokemon.h must continue to #include "Event.h" , since you're inheriting Event , which requires a complete definition.

Use forward declaration, to tell classes the type they need to use will be defined later. You can use forward declaration in situations where the size is know, pointers and references are always the same size regardless of the type they point to so use them.

#ifndef EVENT_H
#define EVENT_H

#include <string>

class Trainer;
class Event
{
protected:
    std::string title;

public:
    Event();
    virtual ~Event();

    virtual void action(Trainer* const trainer) = 0;

};
#endif

then

#ifndef TRAINER_H
#define TRAINER_H

class Pokemon;
class Trainer
{
private:
    Pokemon* const pokemon;
    int numPokemon;
public:
    Trainer();
    ~Trainer();  
};
#endif

then

#ifndef POKEMON_H
#define POKEMON_H
#include "Event.h"
#include <string>
class Pokemon : public Event
{
protected:
    std::string type;
    std::string name;
public:
    Pokemon();
    virtual ~Pokemon();
    virtual bool catchPokemon() = 0;
};
#endif

when using polymorphism (virtual functions) you must always make the base class destructor virtual too. It is also nice to make the derived classes destructor virtual as well, but it is not required.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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