user2906318 user2906318 - 4 months ago 17
C++ Question

“does not name a type” error c++

I have a class declared as below, and I keep getting "Segment does not name a type" error. I've looked at other similar questions but I can't seem to find a solution to my problem. Any help? Thanks in advance! :)

#ifndef ENTRANCE_H
#define ENTRANCE_H
#include "Segment.h"
#include <vector>
#include "Diodio.h"


class Entrance
{
public:
Entrance();
~Entrance();
void operate();


protected:
Segment *givesEntryTo;
std::vector<Diodio> elBooths;
std::vector<Diodio> manBooths;

private:
};

#endif // ENTRANCE_H

Answer

You can avoid a circular include problem by using a forward declaration of a class instead of an #include:

#ifndef ENTRANCE_H
#define ENTRANCE_H
#include <vector>
#include "Diodio.h"

class Segment;    

class Entrance
{
    public:
        Entrance();
        ~Entrance();
        void operate();


    protected:
        Segment *givesEntryTo;
        std::vector<Diodio> elBooths;
            std::vector<Diodio> manBooths;

    private:
};

#endif // ENTRANCE_H

(Entrance.cpp may or may not need to #include "Segment.H" then.)