C++ 在头文件中使用枚举'___'而没有先前的声明?

C++ Use of enum '___' without previous declaration in header file?

本文关键字:声明 枚举 文件 C++      更新时间:2023-10-16

我正在尝试使用枚举来表示交通模拟中的街道和方向。我遇到了一个问题,即尝试将枚举放入构造函数并将其放入 Vehicle 构造函数中。

当我尝试编译车辆时出现错误。 - 在没有事先声明的情况下使用枚举"街道/方向"。 - 在","令牌之前有两个预期的标识符,我在其中声明了车辆构造函数

这是我到目前为止所拥有的。

//Street.h
#ifndef STREET_H
#define STREET_H
enum street
{
    Main = 1,
    Church,
};
#endif
//Direction.h
#ifndef DIRECTION_H
#define DIRECTION_H
enum direction
{
    E = 1,
    W,
    N,  
};
#endif
//Vehicle.h
#ifndef VEHICLE_H  
#define VEHICLE_H
#include "Street.h"
#include "Direction.h"
class Vehicle
{
   private:
    int vehicleNumber;
    int arrivalTime;
    int departureTime;
    enum street;
    enum direction;
   public:
    Vehicle(int, enum, enum, int);
};
#endif

Vehicle(int, enum, enum, int);

这是不正确的,您应该使用所需的枚举类型的名称(在本例中为streetdirection而不是关键字 enum

这些成员变量也是如此(感谢@Unda注意到)。