Search code examples
c++qtenumsenumeration

How declare an enum that is in another class?


I have an enum in one class :

class SettingManager : public QObject
{
    Q_OBJECT
public:
    enum BookDisplayKinds{
        BookDisplay1=0,
        BookDisplay2=1,
    };
    Q_ENUMS(BookDisplayKinds)
};

I want to declare an property in another class

#include<settingmanager.h>
class BookManager : public NetworkManager
{
  private:    
     SettingManager::BookDisplayKinds m_BookDisplayKind;//Error is here
};

but I got below error:

'SettingManager' does not name a type

I add forward delcalration of SettingManager before BookManager

#include<settingmanager.h>
class SettingManager;
class BookManager : public NetworkManager
{
  private:    
     SettingManager::BookDisplayKinds m_BookDisplayKind;//Error is here
};

But now I have below error

'BookDisplayKinds' in 'class SettingManager' does not name a type


Solution

  • You can switch to strong enum available since C++11. I recommend you to put it out of the class.

    // SettingManager.h header
    enum class BookDisplayKinds{
            BookDisplay1=0,
            BookDisplay2=1,
    };
    class SettingManager : public QObject
    {
        Q_OBJECT
    public:
    };
    

    Now it is possible to do a forward declaration like this:

    // BookManager.h header
    enum class BookDisplayKinds; // forward declaration
    class BookManager : public NetworkManager
    {
      private:    
         BookDisplayKinds m_BookDisplayKind;/
    };