2017-04-13 785 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。正在更新它。謝謝。 –