[英]error: request for member in something not a structure or union in this code
I got the error: request for member in something not a structure or union in this code 我收到错误:在此代码中请求成员不是结构或联合
int main() {
char Oberon;
struct Oberon {
short borsa_oro;
short punti_ferita;
short incantesimi;
short pozione_guaritrice;
short veleno;
char armatura;
char arma;
};
Oberon.borsa_oro=10;
Oberon.punti_ferita=5;
Oberon.incantesimi=2;
Oberon.pozione_guaritrice=5;
strcpy (Oberon.armatura, "Vesti del mago");
strcpy (Oberon.arma, "Spada di Oberon");
return 0;
}
what should I do? 我该怎么办?
I think you meant to create something like a player
struct and then create Oberon, who is a struct player. 我认为你的意思是创建一个像player
结构的东西然后创建Oberon,他是一个结构玩家。
You also forgot to associate a block of memory to armatura
and arma
. 你也忘了将一块记忆与armatura
和arma
。
You also tried to create an instance of player before the struct was defined. 您还尝试在定义结构之前创建一个播放器实例。
So I think what you meant was this: 所以我认为你的意思是:
#include <stdio.h>
#include <string.h>
int main(void)
{
struct player {
short borsa_oro;
short punti_ferita;
short incantesimi;
short pozione_guaritrice;
short veleno;
char armatura[50];
char arma[50];
};
struct player Oberon;
Oberon.borsa_oro = 10;
Oberon.punti_ferita = 5;
Oberon.incantesimi = 2;
Oberon.pozione_guaritrice = 5;
strcpy (Oberon.armatura, "Vesti del mago");
strcpy (Oberon.arma, "Spada di Oberon");
printf("Oberon ha %d oro e utilizza la armatura %s\n", Oberon.borsa_oro, Oberon.armatura);
return 0;
}
That's not how structs should be used. 这不是结构应该如何使用。 You also have a char with same name char Oberon;
你也有一个同名char Oberon;
remove or rename it. 删除或重命名它。
Structs just describe a data-type, you still have to initialise them, just like any other; 结构只描述数据类型,你仍然需要初始化它们,就像任何其他类似; example: 例:
struct person {
int age;
...
};
struct person p;
p.age = 10;
To simplify that, use typedef 为简化这一点,请使用typedef
typedef struct person {
int age;
...
} person;
person p;
p.age = 10;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.