开发者

Enumerators Cast

开发者 https://www.devze.com 2023-04-04 09:45 出处:网络
I have en error when try to cast own enumerator to address book values: typedef enum { kACTextFirstName = kABPersonFirstNameProperty, // error: expression is not an integer constant expression

I have en error when try to cast own enumerator to address book values:

typedef enum {
    kACTextFirstName = kABPersonFirstNameProperty, // error: expression is not an integer constant expression
    kACTextLastName = (int)kABPersonLastNameProperty, // error: expression is not an integer constant expression
} ACFieldType;

How to solve the problem?

Thank you.


I need to init my enum using A开发者_高级运维BAddressBook's framework const values such as kABPersonLastNameProperty or kABPersonFirstNameProperty.


In C (unlike in C++), an object declared const, even if it's initialized with a constant expression, cannot be used as a constant.

You didn't bother to show us the declaration of kABPersonFirstNameProperty, but I"m guessing it's declared something like:

const int kABPersonFirstNameProperty = 42;

If you need to use the name kABPersonFirstNameProperty as a constant expression, you can either declare it as a macro:

#define kABPersonFirstNameProperty 42

or as an enumeration constant:

enum { kABPersonFirstNameProperty = 42 };

Note that the enum hack only lets you declare constants of type int.

Likewise for kABPersonLastNameProperty.

(And why do you cast one of them to int, but not the other?)

If that doesn't answer your question, it's because you didn't give us enough information.

0

精彩评论

暂无评论...
验证码 换一张
取 消