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,
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 .
Simple: You have a structure, not a typedef
structure.
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 struct { .. } product;
product apple;
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 };
精彩评论