我需要创建一个联系人列表,并且在我的代码中不断收到此错误:表达式必须具有类类型。
这是代码:
#include<iostream>
#include<string>
using namespace std;
class PhoneApp {
public:
string FirstName;
string LastName;
string PhoneNumber;
string EmailID;
PhoneApp() {
FirstName = "";
LastName = "";
PhoneNumber = "";
EmailID = "";
}
void addContact(){
cout << "Enter your contact's first name: ";
cin >> FirstName;
cout << "Enter your contact's last name: ";
cin >> LastName;
cout << "Enter your contact's phone number: ";
cin >> PhoneNumber;
cout << "Enter your contact's email address: ";
cin >> EmailID;
}
void displayContact(){
cout << "Here's your contact details: " << endl;
cout << "FirstName: " << FirstName << endl;
cout << "LastName: " << LastName << endl;
cout << "PhoneNumber: " << PhoneNumber << endl;
cout << "EmailID: " << EmailID << endl;
}
};
int main(){
PhoneApp myPhoneApp[50];
int index = 0;
while(1){
cout << "Press 1 to add contacts" << endl;
cout << "Press 2 to search for a contact" << endl;
cout << "Anything else to quit" << endl;
int choice;
cin >> choice;
switch(choice){
case 1:{ myPhoneApp[index].addContact();
index++;
break;}
case 2: { cout << "Enter a first name to search for: " << endl;
string search = "";
cin >> search;
for (int i = 0; i < index; i++) {
if(myPhoneApp[50].FirstName[i].compare(index) == 0);
break;
}
}
default: exit(1);
}
}
system("pause");
return 0;
}
错误弹出在
if(myPhoneApp[50].FirstName[i].compare(index) == 0
这里到底是什么问题,我该如何解决?
谢谢您的帮助