Creating Linked Lists in Objective C

前端 未结 3 484
傲寒
傲寒 2020-12-20 15:36
typedef struct {
    NSString *activty;
    NSString *place;
    float latitude;
    float longitude;
} event;

typedef struct {
    event *thing;
    Node *next;
}          


        
3条回答
  •  抹茶落季
    2020-12-20 15:43

    I think you'll find that it doesn't work in C++ (but they might have changed the grammar rules so that it does). You probably mean something more like this:

    struct Node {
        event *thing;
        Node *next;
    };
    

    That works in C++ because Node is equivalent to struct Node if there isn't already something called Node (this sometimes causes puzzling errors when Foo is both a class and the instance method of another class, which happens in some coding styles).

    The fix is to say struct Node. I prefer this; it seems more pure. There are some good reasons to use a typedef (e.g. things like TUInt64 which might have historically been a struct due to lack of compiler support). If you do use a typedef, there's no reason to give the struct a different name since they're in different namespaces (IIRC structs are a "tag namespace").

    The usual typedef version is something like this:

    typedef struct Node Node;
    struct Node {
        event *thing;
        Node *next;
    };
    

    Alternatively, change the file extension to .mm and then it works because you're compiling Objective-C++!

提交回复
热议问题