Xcode 结构帮助
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5372420/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Xcode struct help
提问by MrWolvwxyz
So I am new to programming and even newer to Xcode. I am having trouble using a struct in Xcode. I have gotten to the point where I copied and pasted the code,
所以我是编程的新手,甚至是 Xcode 的新手。我在 Xcode 中使用结构体时遇到问题。我已经到了复制和粘贴代码的地步,
struct product {
int weight;
float price;
} ;
product apple;
from the c++ site, but when I try to declare the apple's weight via apple.weight = 5;
I get errors saying unknown type name 'apple' and expected unqualified Id at .
来自 c++ 站点,但是当我尝试通过声明苹果的重量时,apple.weight = 5;
我收到错误消息,说未知类型名称“苹果”和预期的不合格 Id at 。
回答by Yann Ramin
Simple: You have a structure, not a typedef
structure.
简单:你有一个结构,而不是一个typedef
结构。
You can use it as follows:
您可以按如下方式使用它:
struct product {
int weight;
float price;
};
struct product apple;
void func() {
apple.weight = 12;
}
However, if you use a typedef, you can give your datatype an actual name:
但是,如果您使用 typedef,您可以为您的数据类型指定一个实际名称:
typedef struct { .. } product;
product apple;
回答by aaz
product apple;
apple.weight = 5;
This is valid code inside a function, but not at file scope.
这是函数内的有效代码,但不在文件范围内。
Although at file scope you can initialize it like this:
尽管在文件范围内,您可以像这样初始化它:
product apple = { 5 };