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

时间:2017-04-13 14:18:33

标签: c++ arrays struct

我正在创建一个目录程序,它提示用户输入文件名并将文件读入字符串数组。我在SearchFirstName函数中遇到了麻烦。我收到一个错误:'std :: string'没有名为'userRecord'的成员。我不知道如何解决这个问题,因为声明了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);
 }

1 个答案:

答案 0 :(得分:4)

public class SerialPortsColorReadWriteXML:INotifyPropertyChanged
{

    private SerialPortsColors _LSerialPortsColors;


    public SerialPortsColorReadWriteXML()
    {

        _LSerialPortsColors = new SerialPortsColors();
    }

    public SerialPortsColors LSerialPortsColors
    {
        get
        {
            return _LSerialPortsColors;
        }

        set
        {
            _LSerialPortsColors = value;
            OnProperyChanged("LSerialPortsColors");
        }

    }
    private  string path = "SerialPortsColors.xml";        



    /// <summary>
    /// read the serial ports color XML
    /// </summary>
    /// <returns>In Case of an error return the error</returns>
    public  string ReadXML()
    {            
        XmlSerializer serializer = new XmlSerializer(typeof(SerialPortsColors));
        try
        {
            using (XmlReader reader = XmlReader.Create(path))
            {
                LSerialPortsColors = (SerialPortsColors)serializer.Deserialize(reader);                    
            }
            return "";
        }

        catch  (Exception )
        {
            return "There is an issue with the path of the Serial Ports Color XML";

        }


    }


    public  string WriteXML()
    {
        XmlSerializer serializer = new XmlSerializer(typeof(SerialPortsColors));
        try
        {
            XmlWriterSettings xws = new XmlWriterSettings();
            xws.Indent = true;
            using (XmlWriter writer = XmlWriter.Create(path, xws))
            {



                serializer.Serialize(writer, LSerialPortsColors);
            }

            return "";
        }

        catch
        {
            return  "There is an issue Creating a new XML File";

        }            

    }




    public event PropertyChangedEventHandler PropertyChanged;

    private void OnProperyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

string bookArray[MAX_RECORDS]; 的类型为string.It应为

bookArray

另外

 entryType bookArray[MAX_RECORDS];

normalFirstName = NormalizeString(bookArray[i].userRecord.name.firstName); 不能将bookArray[i]作为成员。userRecord是您声明的变量。 它应该是

userRecord