【问题标题】:“does not name a type” error c++ [duplicate]“没有命名类型”错误c ++ [重复]
【发布时间】:2016-08-05 18:00:13
【问题描述】:

我有一个声明如下的类,但我不断收到“Segment does not name a type”错误。我看过其他类似的问题,但我似乎无法找到解决我的问题的方法。有什么帮助吗?提前致谢! :)

#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

【问题讨论】:

  • 标题之间很可能存在循环依赖关系。
  • 一个猜测:Segment.h 是否直接或间接包含 Entrance.h?
  • 这是一个类定义。类声明为class Entrance;
  • @molbdnilo 我会调查一下谢谢你的建议
  • @DimChtz Segment 是一个代表高速公路段的类,对于入口,我需要一个指针,指向入口进入的路段。

标签: c++ c++11 compiler-errors


【解决方案1】:

您可以通过使用类的前向声明而不是#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 可能需要也可能不需要#include "Segment.H"。)

【讨论】:

    猜你喜欢
    • 2016-01-24
    • 2015-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多