C++ 错误:'std::string'没有成员

c++ error: 'std::string' has no member

本文关键字:成员 string C++ std 错误      更新时间:2023-10-16

我正在创建一个目录程序,该程序提示用户输入文件名并将文件读取到字符串数组中。我的搜索名字功能有问题。 我收到一个错误:"std::string"没有名为"userRecord"的成员。我不确定如何解决这个问题,因为声明了用户记录。

页眉

#include<string>
using namespace std;
enum Title {Mr, Mrs, Ms, Dr, NA};
struct NameType {
   Title title;
   string firstName;
   string lastName;
};
struct AddressType {
   string street;
   string city;
   string state;
   string zip;
};
struct PhoneType {
  int areaCode;
  int prefix;
  int number;
};
struct entryType {
  NameType name;
  AddressType address;
  PhoneType phone;
};
const int MAX_RECORDS = 50;

法典

// string bookArray[MAX_RECORDS];
entryType bookArray[MAX_RECORDS];   //Solution
int bookCount = 0;
void OpenFile(string& filename, ifstream& inData)
{
do {
    cout << "Enter file name to open: ";
    cin >> filename;
    inData.open(filename.c_str());
    if (!inData)
        cout << "File not found!" << endl;
} while (!inData);

if(inData.is_open())
{
    for(int i=0; i<MAX_RECORDS;i++)
    {
        inData>> bookArray[bookCount];
        ++bookCount;
    }
}
}

void SearchFirstName(ifstream& inData)
{
   entryType userRecord; // Declaration of userRecord
   string searchName;
   string normalSearchName, normalFirstName;
   char choice;
   bool found = false;
   cout << "Enter first name to search for: ";
   cin >> searchName;
   for(int i = 0; i < bookCount; ++i){
  normalFirstName = NormalizeString(bookArray[i].userRecord.name.firstName);   
 // Convert retrieved string to all uppercase
    if (normalFirstName == normalSearchName) { // Requested name matches
        PrintRecord(bookArray[i].userRecord.name.firstName);
        cout << "Is this the correct entry? (Y/N)";
        cin >> choice;
        choice = toupper(choice);
        cout << endl;
        if (choice == 'Y') {
            found = true;
            break;
        }
    }
}
// Matching name was found before the end of the file
if (inData && !found){
    cout << "Record found: " << endl;
    PrintRecord(userRecord);
    cout << endl;
}
else if (!found)   // End of file. Name not found.
{
    cout << searchName << " not found!" << endl << endl;
}
// Clear file fail state and return to beginning
inData.clear();
inData.seekg(0);
 }
 string bookArray[MAX_RECORDS];

bookArray是字符串类型。它应该是

 entryType bookArray[MAX_RECORDS];

normalFirstName = NormalizeString(bookArray[i].userRecord.name.firstName); 

bookArray[i]不能userRecord作为成员。userRecord是您声明的变量。它应该是

normalFirstName = NormalizeString(bookArray[i].name.firstName);