2017-04-13 2270 views
-2

我正在创建一个目录程序,提示用户输入文件名并将文件读入字符串数组。我在我的SearchFirstName函数中遇到了麻烦。我得到一个错误:'std :: string'没有名为'userRecord'的成员。我不知道如何解决这个问题,因为userRecord被声明。C++错误:'std :: string'没有成员

部首

#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); 
} 
+0

什么是'bookArray'类型? – NathanOliver

+0

我想bookArray是一个字符串数组?而string没有成员名称userRecord。那么什么不清楚? – basslo

+0

'bookArray'应该是一个'entryType'的数组,我认为它的类型是'string',这就是抛出错误的原因。向我们展示'bookArray'的定义。 –

回答

4
string bookArray[MAX_RECORDS]; 

bookArray是类型string.It的应该是

entryType bookArray[MAX_RECORDS]; 

另外

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

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

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

'entryType'仍然没有名为'userRecord'的成员。 –

+0

@ n.m。正在更新它。谢谢。 –

相关问题