Forward Declaring enum class not working

后端 未结 1 1448
南旧
南旧 2021-01-04 02:39

In State.h I have

enum class StateID : unsigned int;

In State.cpp I have

enum class StateID : unsigned int
{
    NullID = 0         


        
相关标签:
1条回答
  • 2021-01-04 02:40

    That's because only States.cpp knows what members exist inside enum class ID.

    Files that include States.hpp only know that the enum class ID is the size of an unsigned int, and that is all.

    You will need to make your enumeration values available in a header so that any file that does need to know about the enumerated values (e.g. MainMenuID) has them available.

    You could make a separate header just for forwarding, maybe called StateFwd.hpp and then rename your State.cpp to State.hpp.

    Example of where/how you might forward declare

    I've updated my answer with an example, following the discussion we had in the comments.

    fruit.hpp

    Anyone who chooses to include this header will know what kind of fruits exist.

    #ifndef FRUIT_HPP
    #define FRUIT_HPP
    
    enum class Fruit
    {
        APPLE,
        ORANGE,
        BANANA
    };
    
    #endif
    

    village.hpp

    The village is full of people driven by their lust for fruit.

    #ifndef VILLAGE_HPP
    #define VILLAGE_HPP
    
    enum class Fruit;
    
    namespace farmer
    {
        bool is_fruit_for_sale(Fruit fruit);
        float get_cost_of_fruit(Fruit fruit);
    }
    
    namespace blind_but_greedy_merchant
    {
        bool sell_fruit(Fruit fruit, float money);
    }
    
    namespace peasant
    {
        inline bool buy_fruit(Fruit fruit, float money)
        {
            return blind_but_greedy_merchant::sell_fruit(fruit, money);
        }
    }
    
    #endif
    

    farmer.cpp

    This farmer only grows apples and oranges, so he never has bananas for sale

    #include "fruit.hpp"
    
    namespace farmer
    {
        bool is_fruit_for_sale(Fruit fruit)
        {
            switch ( fruit ) {
            case Fruit::APPLE:
            case Fruit::ORANGE:
                return true;
            case Fruit::BANANA:
                return false;
            }
            return false;
        }
    
        float get_cost_of_fruit(Fruit fruit)
        {
            switch ( fruit ) {
            case Fruit::APPLE:
                return 1.00f;
            case Fruit::ORANGE:
                return 2.50f;
            case Fruit::BANANA:
                return 200.0f;
            }
            return 0.0f;
        }
    }
    

    merchant.cpp

    This merchant has gone blind from greed. He can no longer see what kind of fruit he is selling. He still knows how to deal with the farmer though, passing on customer requests to the farmer, while adding a steep profit margin for all fruit. This is why fruit.hpp is not included.

    #include "village.hpp"
    
    namespace blind_but_greedy_merchant
    {
        bool sell_fruit(Fruit fruit, float money)
        {
            if ( !farmer::is_fruit_for_sale(fruit) ) {
                return false;
            }
    
            float inflatedcost = farmer::get_cost_of_fruit(fruit) * 3;
    
            if ( money < inflatedcost ) {
                return false;
            }
    
            return true;
        }
    }
    

    example.cpp

    This pulls it all together. In our example, we want the peasant to go buy some fruit. We know exactly what kind of fruit we want; a BANANA. Therefore we need to include fruit.hpp, otherwise we could not tell the peasant to go and buy a BANANA for us.

    The only two people in this scenario who know what kind of fruit exist are us (example.cpp) and the farmer (farmer.cpp). The peasant doesn't even need to know. It's like we've given him a folded piece of paper that says what fruit we want, but we've told him not to look at it. And he just hands it to the merchant, who can't read, so he just passes it on to the farmer.

    #include "village.hpp"
    
    #include "fruit.hpp"
    
    int main()
    {
        peasant::buy_fruit(Fruit::BANANA, 25.0f);
    
        return 0;
    }
    
    0 讨论(0)
提交回复
热议问题