了解嵌套结构 [英] Understanding Nested Structures
问题描述
我目前正在研究需要嵌套结构的程序.虽然,我不确定我是否理解.如果有人可以的话,我想请您帮忙.学习C ++的第一周,所以不要给我辛苦的时间:P
I'm currently working on a program that requires a nested structure. Though, I am not sure I understand it. I'd like your help with this if anyone could. First week learning C++ so don't give me a hard time :P
我应该创建一个包含两个字符串成员(第一和最后一个)的Person结构.创建一个包含四个字符串成员(街道,城市,州和邮政编码)的地址结构.并创建一个嵌套结构Employee,它由三个成员组成.个人会员 名为name,一个名为homeAddress的地址成员,以及一个名为eid的int成员.
I'm supposed create a Person structure containing two string members, first and last. Create an Address structure containing four string members, street, city, state, and zipcode. And also create a nested structure, Employee, that consists of three members. A Person member named name, an Address member named homeAddress and an int member named eid.
我认为我已经正确完成了大多数操作,但是由于某种原因,我的地址homeAddress下不允许输入不完整的类型.另外,当它说创建嵌套结构"Employee"时,是否意味着我必须在某个地方声明Employee?
I think I've done most of it correctly, but I am getting an incomplete type is not allowed under my Address homeAddress for some reason. Also, when it says create a nested structure "Employee" does that mean I have to declare Employee somewhere?
这是我到目前为止所拥有的,谢谢.
Here is what I have so far, thanks in advance.
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
struct Person {
string first;
string last;
};
struct Address {
string street;
string city;
string state;
string zipcode;
Person name;
Address homeAddress;
int eid;
};
推荐答案
您的代码即将完成.应该是:
Your code was almost complete. It should be:
struct Person {
string first;
string last;
};
struct Address {
string street;
string city;
string state;
string zipcode;
};
struct Employee {
Person name;
Address homeAddress;
int eid;
};
现在这里的误称是嵌套也可能暗示可见性或范围.因此,如果您想在Employee
中定义结构Address
和Person
,则如下所示:
Now the misnomer here is that nested can also imply visibility or scope. Hence, if you wanted to define the structure Address
and Person
within Employee
, it would look like this:
struct Employee {
struct Address {
//..
};
struct Employee {
//..
};
Person name;
Address homeAddress;
int eid;
};
这样,您就可以使Person
和Address
的范围仅与Employee
的范围相同.
That way, you make the scope of Person
and Address
native to that of Employee
alone.
这篇关于了解嵌套结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!