2
我试图用Horowitz,Sahni和Anderson-Freed的“C语言中的数据结构基础”研究c语言。在关于结构和联合的部分,有一个练习问题,我必须创建一个包含联合的结构,以根据每个结构的值包含附加信息。使用联合将标记域值赋给结构
的代码如下:在发生
/*
Modify the humanBeing structure so that we can include different information
based on marital status. Marital status should be an enumberated type with field
single, married, widowed, divorced. Use a union to include different information
based on marital status as follows:
- single. no information needed.
- married. include a marriage date field.
- widowed. include marriage date and death of spouse date fields.
- divorced. include divorce date and number of divorces fields.
Assign values to the fields for some person fo type humanBeing.
*/
#include <stdio.h>
#include <string.h>
typedef struct date {
int year;
int month;
int date;
};
typedef struct wInfo {
date dMarriage;
date dDeath;
};
typedef struct dInfo {
date dDivorce;
int nDivorce;
};
typedef struct mType {
enum tagField { single, married, widowed, divorced } maritalStatus;
union {
date dMarriage;
wInfo death;
dInfo divorce;
} u;
};
typedef struct humanBeing {
char name[10];
int age;
float salary;
mType maritalInfo;
};
void main() {
humanBeing person1;
strcpy(person1.name, "Phil");
person1.age = 20;
person1.salary = 4800;
person1.maritalInfo.maritalStatus = married;
person1.maritalInfo.u.dMarriage.year = 1991;
person1.maritalInfo.u.dMarriage.month = 6;
person1.maritalInfo.u.dMarriage.date = 10;
}
我的错误:
person1.maritalInfo.maritalStatus = married;
的 “嫁” 具有读取错误:标识符 “已婚” 不明。
但是我没有在创建mType时识别它吗?
我在网上找到了教科书中所有问题的解决方案。我将我的代码与解决方案相比较。我看不到我在做什么不同,所以我复制并将解决方案的代码粘贴到我的Visual Studio中,但同样的错误发生。这让我想我的视觉工作室设置可能有问题。但错误清楚地表明这是一个语法错误?
我相信我犯了一个愚蠢的错误,但你们能帮忙吗? 谢谢。
感谢您的帮助。我已经把我的main改成了'int main(void)'。但我认为结构的名称要么在括号之前,要么在两者之后。我错了吗?我根据你的建议改变了代码,但同样的错误仍然:( – hollaholl