stackunderflow
stackunderflow

Reputation: 917

error enum has no member when using macro

I want create two enums: DerivedBirdType and BasicBirdType. BasicBirdType has members named like BCT_*. DerivedBirdType has all members in BasicBirdType but with different prefix DCT_*, and it also has members not in BasicBirdType.

For example, BasicBirdType has two members, *BCT_waterfowl* and *BCT_loons*. and DerivedBirdType will have *DCT_waterfowl*, *DCT_loons* and a new member *DCT_Obama*.

I have 3 files. Bird.h declares the enums structure and token-parsing operator which reuse the common names. DerivedBirdType.h reuse the members in BasicBirdType.h and add extra members.

The problem is BasicBirdType now has no member "BCT_waterfowl" when I tried to get BCT_*, while DerivedBirdType works fine.

BasicBirdType bird1 = BasicBirdType::BCT_waterfowl; // enum `BasicBirdType` now has no member "BCT_waterfowl"
DerivedBirdType bird2 = DerivedBirdType::DCT_waterfowl; // works fine

Bird.h:

#pragma once
#define BASIC_BIRD_TYPE(type) BCT_##type,
#define DERIVED_BIRD_TYPE(type) DCT_##type,

namespace Bird
{
    enum DerivedBirdType
    {
        #include "DerivedBirdType.h"
        TotalDerivedBirdTypes
    };

    enum BasicBirdType
    {
        #include "BasicBirdType.h"
        TotalBasicBirdTypes
    };
}

DerivedBirdType.h:

#pragma once
#define BASIC_BIRD_TYPE(type) DERIVED_BIRD_TYPE(type)

#include "BasicBirdType.h"
DERIVED_BIRD_TYPE(Obama)

BasicBirdType.h:

#pragma once
BASIC_BIRD_TYPE(waterfowl)
BASIC_BIRD_TYPE(loons)

Upvotes: 1

Views: 425

Answers (1)

Atif Alam
Atif Alam

Reputation: 26

your problem is that the header file "BasicBirdType.h" is already included when you included header file "DerivedBirdType.h", Therefore when you included BasicBirdType in the Basic enum, the compiler removes it as it being already included

enum BasicBirdType
{
    #include "BasicBirdType.h"    // no effect, file already included
    TotalBasicBirdTypes
};

This is the reason that the BasicBirdType enum is empty apart from TotalBasicBirdTypes

Upvotes: 1

Related Questions