我在 objective-c 中创建此 c 结构时遇到问题。
typedef struct huffman_node_tag
{
unsigned char isLeaf;
unsigned long count;
struct huffman_node_tag *parent;
union
{
struct
{
struct huffman_node_tag *zero, *one;
};
unsigned char symbol;
};
} huffman_node;
我在“unsigned char symbol variable”上方的 union 类型末尾和结构类型末尾收到此警告
warning: declaration does not declare anything
然后当我做这样的事情时:
huffman_node *p = (huffman_node*)malloc(sizeof(huffman_node));
p->zero = zero;
我得到这个编译错误:
error: 'huffman_node' has no member named 'zero'
为什么这不起作用?我设置不正确吗?有没有人以前经历过这种情况?
最佳答案
typedef struct huffman_node_tag
{
unsigned char isLeaf;
unsigned long count;
struct huffman_node_tag *parent;
union
{
struct
{
struct huffman_node_tag *zero, *one;
}; // problematic here!
unsigned char symbol;
}; // another problem here!
} huffman_node;
根据用于解释代码的 C 方言/编译器,您可能不允许在没有名称的情况下声明 struct
或 union
。试着给他们起名字,看看会发生什么。或者,您可能想尝试更改您正在使用的 C 方言。
关于iphone - 在 objective-c 中使用 c 库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1936789/