Expected unqualified-id

Implement a class Person with the following data members:
• name (string) - name of this person
• age (integer) – age of this person

Write a program that reads in a list of names and ages and stores them in a one-dimensional array of Person objects. The maximum number of names that will be entered is 100 names. After reading in the list of names and ages, sort the list of people from the youngest (lowest age) to oldest (highest age). Then print out the name and age for each person in the sorted list.

I have the code that I have posted below so far. I am getting one error in my int main() for an Expected unqualified- id. Not sure what exactly that means and how to go about fixing it. Any help, hints, or suggestions would be greatly appreciated.

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;

class Person
{
public:
Person();
void arrInit();
void arrInput();
void arrSort();
void arrOutput();

private:
string name; //name of the person ??
int age; //age of the person ??
static const int arrSize = 100; //how many names
int arrAge[arrSize]; //age of the person - array
string arrName[arrSize]; //name of the person - array
};


Person::Person()
{
name = " ";
age = 0;
};


void Person::arrInput()
{
for (int s=0; s < arrSize; s++)
{

cout << "Enter name (-1 to stop): ";
cin >> arrName[s];

//if (arrName[s] == -1)
//{
//break;
//}
cout << "Enter age of " << arrName[s] << ": ";
cin >> arrAge[s];
} //end for
}

void Person::arrOutput()
{
for (int j=0; j < arrSize; ++j)
{
cout << "Name: "<< arrName[j] << "," << "age: " << arrAge[j] << endl;
}
}

//insertion sort - chapter 7 - swap positions
void Person::arrSort()
{
int insertNum = 0;
string insertName;

for (int m = 0; m < arrSize; ++m)
{
for (int k = 0; k < (arrSize-1); ++k)
{
insertNum = arrAge[k+1];
insertName = arrName[k+1];

if (insertNum < arrAge[k])
{
arrAge[k+1] = arrAge[k];
arrAge[k] = insertNum;
arrName[k+1] = arrName[k];
arrName[k] = insertName;
}//end if
}//end nested inner for
}//end outer for
}//end function

//initialize array of age
void Person::arrInit()
{
for (int i=0; i < arrSize; i++)
{
arrAge[i] = 0;
}
}

int main()
{
Person person;

Person.arrInput();

system("PAUSE");
return 0;
}
Person is the type name, person is the object name. To call a member function, you need to provide the name of an object.

So,

1
2
3
4
5
int main()
{
    Person person;
    person.arrInput();
}


hints, or suggestions

Don't use arrays. In this case, use a map<string, age> or a vector of pairs (so you can sort it the way you need), or even a multimap<age, string> so the compiler sorts for you.
Don't forget to uncomment your "-1" check. (and use "-1", not -1 there)
Use member initializers in your constructors

Last edited on
In function main(), change this line:
 
    Person.arrInput();

to this:
 
    person.arrInput();

The lowercase version is the particular object belonging to the class Person

Looking at the above comments regarding arrays etc. I'm tempted to say there should be both a class Person and a class People. I get the impression here the class is trying to be both.

Actually, re-reading the description of the assignment, it's looks like these data items don't belong inside class Person.
1
2
3
    static const int arrSize = 100; //how many names
    int arrAge[arrSize]; //age of the person - array
    string arrName[arrSize]; //name of the person - array 

and in any case it specifically refers to a single one-dimensional array of Person objects.
Last edited on
Indeed, that Person has many names and has many ages!
Topic archived. No new replies allowed.