枚举的非整型底层类型

时间:2022-07-05 16:08:02

I'm working on integrating objective-git into my project, but when I include their headers in my sources, I get these errors on several of their enum declarations:

我正在将objective-git集成到我的项目中,但是当我将它们的头信息包含在我的源文件中时,我就会在它们的enum声明中发现这些错误:

objective-git/Classes/GTRepository.h:57:16: Non-integral type 'git_reset_t' is an invalid underlying type

objective-git /类/ GTRepository。h:57:16:非整型‘git_reset_t’是一个无效的底层类型

Here's the code in question:

这里的代码是这样的:

typedef enum : git_reset_t {
    GTRepositoryResetTypeSoft = GIT_RESET_SOFT,
    GTRepositoryResetTypeMixed = GIT_RESET_MIXED,
    GTRepositoryResetTypeHard = GIT_RESET_HARD
} GTRepositoryResetType;

I changed git_reset_t to NSUInteger (typedef'd to unsigned long), and that got it to compile, but of course I'd rather not have to change the library files.

我将git_reset_t更改为NSUInteger (typedef转换为unsigned long),这使它得以编译,但我当然不希望更改库文件。

Objective-git compiles just fine in its own project, and I can't find any significant difference in the compiler settings between that project and mine. What could I be missing?

Objective-git在它自己的项目中编译得很好,我在这个项目和我的项目之间的编译器设置中找不到任何显著的不同。我还会错过什么呢?

This is with Xcode 4.5, compiling with Apple llvm 4.1.

这是Xcode 4.5,与Apple llvm 4.1一起编译。

Update: The clue I missed was that the error only happened on a .mm file, and .m files were fine, so somehow the underlying enum type doesn't work in C++ (even if I enable C++11). As a workaround I put a fake minimal @interface declaration for the one objective-git class I use in that file so I don't have to include the headers, but I'd still like to find a cleaner solution.

更新:我漏掉的线索是,错误只发生在.mm文件上,而.m文件很好,所以底层enum类型在c++中不工作(即使我启用了c++ 11)。作为一个解决方案,我为我在那个文件中使用的一个objective-git类添加了一个伪最小化的@interface声明,这样我就不必包含header了,但我仍然希望找到一个更干净的解决方案。

1 个解决方案

#1


1  

Google turns up this file containing this:

谷歌打开包含以下内容的文件:

typedef enum {
    GIT_RESET_SOFT  = 1, /** Move the head to the given commit */
    GIT_RESET_MIXED = 2, /** SOFT plus reset index to the commit */
    GIT_RESET_HARD  = 3, /** MIXED plus changes in working tree discarded */
} git_reset_t;

This is an old-style enumeration with int being the underlying type. But it's not an int, it's a distinct type. And it's not integral and it can't be an underlying type for a new-style enumeration.

这是一个旧式的枚举,int是底层类型。但它不是int型的,它是一种不同的类型。它不是整数,也不可能是新类型枚举的基础类型。

The fix is to use typedef enum : int or if you can use C++ and want to be extra expository,

解决方法是使用typedef enum: int或者如果您可以使用c++并希望成为额外的解释性,

typedef enum : std::underlying_type< git_reset_t >::type

I haven't tried, but you could also try this in ObjC without C++:

我没有尝试过,但是你也可以在没有c++的ObjC中尝试一下:

typedef enum : __underlying_type( git_reset_t )

#1


1  

Google turns up this file containing this:

谷歌打开包含以下内容的文件:

typedef enum {
    GIT_RESET_SOFT  = 1, /** Move the head to the given commit */
    GIT_RESET_MIXED = 2, /** SOFT plus reset index to the commit */
    GIT_RESET_HARD  = 3, /** MIXED plus changes in working tree discarded */
} git_reset_t;

This is an old-style enumeration with int being the underlying type. But it's not an int, it's a distinct type. And it's not integral and it can't be an underlying type for a new-style enumeration.

这是一个旧式的枚举,int是底层类型。但它不是int型的,它是一种不同的类型。它不是整数,也不可能是新类型枚举的基础类型。

The fix is to use typedef enum : int or if you can use C++ and want to be extra expository,

解决方法是使用typedef enum: int或者如果您可以使用c++并希望成为额外的解释性,

typedef enum : std::underlying_type< git_reset_t >::type

I haven't tried, but you could also try this in ObjC without C++:

我没有尝试过,但是你也可以在没有c++的ObjC中尝试一下:

typedef enum : __underlying_type( git_reset_t )