2014-02-16 23 views
3

我试图在目标C.目标C - 在头文件中使用typedef结构

创建一个LinkedList在.h文件我想使用的代码来创建一个节点:

@interface AALinkedList : NSObject 
{ 
    typedef struct Node 
    { 
     int data; 
     struct Node *next; 
    } Node; 
} 

这给我一个错误说Type name does not allow storage class to be specified

这是什么意思?我该如何解决它?

+1

你不能在Objective-C类的数据部分定义一个新类型。 'storage class'就是'typedef',因为它是一个类似'static'或'auto'的存储类。 – user3125367

回答

4
typedef struct Node { 
    int data; 
    Node *next; 
} Node; 

@interface AALinkedList : NSObject 
{ 
    Node node; 
    // or Node *node; 
} 
+0

为什么它会超出@interface? –

+1

因为@interface {与struct {'几乎相同,并且你没有在结构体中定义新的类型。即使你有能力,这也是毫无意义的,因为这个名字的范围只限于那个结构。 – user3125367

0

您不能在.h文件中声明typedef。在.m中声明它。这应该让你去。把方法签名上.h

你可以创建一个简单的LinkedList类这样。

@interface LinkedNode : NSObject 
    @property (nonatomic, strong) id nextNode; 
@end 
then you use it as you would expect: 

id currentNode = myFirstNode; 
do { 
    [currentNode someMessage]; 
} 
while(currentNode = currentNode.nextNode); 
+0

为什么?并在.m文件中的位置? –

+0

编辑我的答案。 – 2014-02-16 09:36:01

+0

这是不正确的,你可以在.h或.m文件的正确位置*声明*。虽然使用属性比结构更好。 – Wain