user2990094
user2990094

Reputation: 3

Error: Must have class type

I need to create a list of contacts and I keep getting this error in my code: expression must have class type.

Here's the code:

#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;
}

The error pops up at

if(myPhoneApp[50].FirstName[i].compare(index) == 0

What exactly is the problem here and how do I fix it?

Thanks for the help

Upvotes: 0

Views: 287

Answers (3)

kfsone
kfsone

Reputation: 24269

There are several small things coming together here to cause you a problem.

if(myPhoneApp[50].FirstName[i].compare(index) == 0);
    break;

Firstly: You have a stray semicolon on the end of the "if" line

if();

means "do the test, and then forget about it. always do the thing on the next line".

Secondly, you are only ever checking one element of your contacts list

myPhoneApp[50]

surely you mean't

myPhoneApp[i]

Next, myPhoneApp[50].Firstname resolves to a single instance of std::string, you then try to index the letters in that name and compare them with the number of entries in your index.

myPhoneApp[50].FirstName[i].compare(index)

What you presumably mean is

if(myPhoneApp[i].FirstName == search)
    break;

---- Edit ----

You've given your member variables of "myPhoneApp" upper-camelcase names, just like your class names. This is going to confuse you, and it means you can't name your member variables to match their type:

PhoneApp PhoneApp;

is a compile error.

A common practice is to give member variables distinctive names by adding a prefix ("m_" for member) or suffix (some places add "_" to indicate a member variable).

E.g.

class PhoneApp {
public:

    string m_FirstName;
    string m_LastName;
    string m_PhoneNumber;
    string m_EmailID;

    PhoneApp() {
        m_FirstName = "";
        m_LastName = "";
        m_PhoneNumber = "";
        m_EmailID = "";
    }
};

or "m_firstName", "m_lastName" etc.

Upvotes: 0

nhgrif
nhgrif

Reputation: 62072

cout << "Enter a first name to search for: " << endl;
string search = "";
cin >> search;
for (int i = 0; i < index; i++) {       
    if(myPhoneApp[i].FirstName.compare(search) == 0) {
        //do stuff
        break;
    }
}

Several things were going wrong.

First off, you need to compare the entire FirstName string to search, not individual characters of FirstName.

Second, you need to iterate through the contacts in myPhoneApp[], not just keep checking different characters of the FirstName string in index 50, which isn't even necessarily set.

Third, no string.compare() overloads take a single int as an argument. What you're looking for is the method to compare two strings, which is what my answer will do.

Fourth, an issue you didn't get to yet... you had a semicolon after your if statement, so regardless of the condition of the if statement, nothing really executes... and you'd just hit that break; after a single iteration, no matter what.

dashblinkenlight's answer explains why the error message was what it was, mine shows you how to fix your program.

Upvotes: 1

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 727137

There are multiple errors in your code, but the one that you are pointing to is as follows:

  • myPhoneApp[50].FirstName is a string
  • Strings consist of characters. You access characters in a string using a subscript operator [i]
  • Characters are primitives.
  • You can access members using a dot . on classes and structures, but not on primitives
  • Since myPhoneApp[50].FirstName[i] is a char, and since char is a primitive, using a dot on it is invalid.

Upvotes: 3

Related Questions