我遇到一个问题,编译器尖叫着枚举没有命名类型。 我在这里查看了其他帖子(enum does not name type),但大多数是因为循环依赖问题。
输入/RegisteredKeyEventCallback.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 就是所有代码。
仍然出现编译错误 错误:结构 RegisteredKeyEventCallback 中的 KEY_EVENT_TYPE_ENUM 未命名类型。
如有任何帮助,我们将不胜感激。谢谢。
编辑:感谢您的建议,多亏了您,我设法解决了
输入/RegisteredKeyEventCallback.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
类进行前向声明,您只对类本身进行前向声明,而不是其成员。您需要 RegisteredKeyEventCallback
类 及其成员的完整定义 HashFunctions.h
头文件,这意味着您将获得循环依赖你需要用其他方法解决。
关于c++ - 枚举不命名类型,(无循环依赖),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37653967/