C++ 嵌套结构声明和如何访问它们
我在使用指向嵌套结构的指针以及在类的成员函数中使用这些结构中的变量时遇到问题 我声明了这些变量。让我知道它们是否被宣布与结构嵌套相关C++ 嵌套结构声明和如何访问它们,c++,data-structures,C++,Data Structures,我在使用指向嵌套结构的指针以及在类的成员函数中使用这些结构中的变量时遇到问题 我声明了这些变量。让我知道它们是否被宣布与结构嵌套相关 const int MAX_ACCOUNT_COUNTRY = 36; const int MAX_ACCOUNT_CITY = 50; const int MAX_ACCOUNT_NAME = 10; const int MAX_NUMBER = 16; const int MAX_ACCOUNT_ADDRESS = 25;
const int MAX_ACCOUNT_COUNTRY = 36;
const int MAX_ACCOUNT_CITY = 50;
const int MAX_ACCOUNT_NAME = 10;
const int MAX_NUMBER = 16;
const int MAX_ACCOUNT_ADDRESS = 25;
const int MAX_ACCOUNT_EMAIL = 50;
const int MAX_OFFICE_HOURS = 20;
const int businessID = 0;
const int residentalID = 1;
typedef char accountCountry[MAX_ACCOUNT_COUNTRY + 1];
typedef char accountCity[MAX_ACCOUNT_CITY + 1];
typedef char accountName[MAX_ACCOUNT_NAME + 1];
typedef char phoneNumberFormat[MAX_NUMBER + 1];
typedef char accountAddress[MAX_ACCOUNT_ADDRESS + 1];
typedef char accountEmail[MAX_ACCOUNT_EMAIL + 1];
typedef char officeHours[MAX_OFFICE_HOURS + 1];
typedef phoneBook * phonebookPtr;
public:
Phonebook(const int numPages);
~Phonebook();
void addAccount(const int);
void removeAccount(const int);
void editAccount(const int);
void viewPhonebook();
private:
struct phoneBook{
struct businessAccountEntry
{
const int businessID;
accountCountry businessCountry;
accountCity businessCity;
accountName businessName;
accountName accountHolder;
accountAddress businessAddress;
accountAddress mailingAddress;
phoneNumberFormat phoneNumber;
phoneNumberFormat faxNumber;
accountEmail businessEmail;
officeHours businessOfficeHours;
};
struct residentialAccountEntry
{
const int residentialID;
accountName residentName;
accountAddress mailingAddress;
phoneNumberFormat phoneNumber;
};
};
phonebookPtr thePhonebook;
int counterID = 0;
Phonebook();
Phonebook(Phonebook &);
};
如何访问我的成员函数类中结构businessAccountEntry
中的变量?给它们取一个如下名称
struct phoneBook{
struct businessAccountEntry
{
const int businessID;
accountCountry businessCountry;
accountCity businessCity;
accountName businessName;
accountName accountHolder;
accountAddress businessAddress;
accountAddress mailingAddress;
phoneNumberFormat phoneNumber;
phoneNumberFormat faxNumber;
accountEmail businessEmail;
officeHours businessOfficeHours;
} businessAccount;
struct residentialAccountEntry
{
const int residentialID;
accountName residentName;
accountAddress mailingAddress;
phoneNumberFormat phoneNumber;
} residentialAccount;
};
然后使用它:thePhonebook->residentialAccount.residentialID
给他们一个像这样的名字
struct phoneBook{
struct businessAccountEntry
{
const int businessID;
accountCountry businessCountry;
accountCity businessCity;
accountName businessName;
accountName accountHolder;
accountAddress businessAddress;
accountAddress mailingAddress;
phoneNumberFormat phoneNumber;
phoneNumberFormat faxNumber;
accountEmail businessEmail;
officeHours businessOfficeHours;
} businessAccount;
struct residentialAccountEntry
{
const int residentialID;
accountName residentName;
accountAddress mailingAddress;
phoneNumberFormat phoneNumber;
} residentialAccount;
};
并且使用它:
thePhonebook->residentialAccount.residentialID
谢谢,在我的CS专业的任何课程中都没有教过这一点,在我拥有的任何书籍中都没有。我如何使用phonebookPtr->residentialAccount.residentialID
,并将其分配给一系列结构中的特定索引?我需要创建一个临时变量来保存它然后存储它吗?也许你应该使用std::vector而不是phonebook的ptr。我不太了解向量,但是有没有一种方法可以使用结构数组来实现呢@JeromeThank you,就CS专业而言,在我的任何一门课上都没有教过这一点,在我的任何一本书中也没有教过。我如何使用phonebookPtr->residentialAccount.residentialID
并将其分配给一系列结构中的特定索引?我需要创建一个临时变量来保存它然后存储它吗?也许你应该使用std::vector而不是phonebook的ptr。我不太了解向量,但是有没有一种方法可以使用结构数组来实现呢@杰罗姆