枚举不命名类型(无循环依赖关系)

Enum does not name type, (no cyclic dependecy)

本文关键字:循环 依赖 关系 类型 枚举      更新时间:2023-10-16

我遇到了一个问题,编译器尖叫着枚举不命名类型。我在这里查看了其他帖子(枚举不命名类型),但其中大多数是因为循环依赖问题。

input/RegisterKeyEventCallback.h 中的代码

#pragma once
#include "UserDefinableInput.h"
#include "../Game/Other/CallBack/CallBack.h"
#include "../Containers/LinkedList/LinkedList.h"
#include "../HashFunctions/HashFunctions.h"
using namespace std;
class InputManager;
/**
* @brief structure for saving all callbacks that belong to one ID.
*/
struct RegisteredKeyEventCallback
{
    enum KEY_EVENT_TYPE_ENUM
    {
        KEY_PRESS, /**< When key was released and is pressed. Fired only once */
        WHILE_KEY_DOWN, /**< Fired while the key is down. Multiple times */
        KEY_RELEASE, /**< When key was pressed and is released now. Fired only once */
        _NULL
    };
    /**
    * @brief Creates new empty instance
    */
    RegisteredKeyEventCallback();
    ~RegisteredKeyEventCallback();
  ...
};

哈希函数.h

#pragma once
struct RegisteredKeyEventCallback;
struct KEY_EVENT_TYPE_ENUM_Hasher
{
   size_t operator()(const RegisteredKeyEventCallback::KEY_EVENT_TYPE_ENUM& k) const;
};

对不起,如果我是瞎子,但我看不到它。循环依赖不应该存在,正如我正向声明的那样,我提供的hashfunctions.h是所有代码。

仍然,得到编译错误错误:结构中的KEY_EVENT_TYPE_ENUM RegisterKeyEventCallback 不命名类型。

任何帮助将不胜感激。谢谢。

编辑:感谢您的建议,多亏了您,我设法解决了它

input/RegisterKeyEventCallback.h 中的代码

#pragma once
#include "UserDefinableInput.h"
#include "../Game/Other/CallBack/CallBack.h"
#include "../Containers/LinkedList/LinkedList.h"
#include "../HashFunctions/HashFunctions.h"
using namespace std;
class InputManager;
struct HashFunctions; //<---------------------------
/**
* @brief structure for saving all callbacks that belong to one ID.
*/

哈希函数.h

#pragma once
#include "../Input/RegisteredKeyEventCallback.h"
struct KEY_EVENT_TYPE_ENUM_Hasher
{
    size_t operator()(const RegisteredKeyEventCallback::KEY_EVENT_TYPE_ENUM& k) const;
};

你是对的,你没有循环依赖,但这不是这里的问题。问题是,在HashFunctions.h中,编译器不知道RegisteredKeyEventCallback::KEY_EVENT_TYPE_ENUM是什么。

通过对RegisteredKeyEventCallback类进行前向声明,您只对类本身进行前向声明,而不是对其成员进行前向声明。您需要在 HashFunctions.h 头文件中RegisteredKeyEventCallback及其成员的完整定义,这意味着您将获得需要以其他方式解决的循环依赖项。