Hello everyone. I'm a new member here but have an issue that I've searched and searched for and just cannot figure out how to resolve. Based off of the issues that I was able to correct, Daniweb seemed like the best place to ask.
I'm making my first attempt to separate my interface from an implementation on a small project for school. My instructor didn't specify us to do this, but I'm attempting so that I can teach myself some things that I don't know how to do.
I've created a Person class "Person.cpp" which contains my function prototypes. I also have "Person.h" containing my member function definitions. Lastly I have my main program.
Here are my goals:
Create an array of objects with names (complete).
Enter the age and height of the objects (complete).
Pass array of objects to functions getOldest, getYoungest, getTallest, and getShortest and return the name of these objects with these characteristics (incomplete).
Here is where I have the problem. I've successfully created both my interface and implementation for getOldest. I can't figure out how to call this function in main.cpp while sending my array of objects.
Person.h
/*
* File: Person.h
* Person definition - Presents Person's public interface
* Does not reveal implementation of Person.cpp defined member functions
* Created on March 14, 2012, 6:59 PM
*/
#include <string>
using namespace std;
//Person class definition
class Person
{
public:
Person (string); //Constructor - sets the name of the person
void setPerson (string); //Function - sets the name of the person
string getPerson ();
void setAge (int); //Function - sets the age of the person
int getAge (); //Function - gets age of the person
void setHeight (int); //Function - sets the height of the person
int getHeight (); //Function - gets the height of the person
string getOldest (Person[]); //Function - gets oldest person
string getYoungest (Person[]); //Function - gets youngest person
string getTallest (Person[]); //Function - gets tallest person
string getShortest (Person[]); //Function - gets shortest person
private:
string personName;
int personAge, personHeight;
}; //End class Person
Person.cpp
/*
* File: Person.cpp
* Person member-function definitions - Contains member function implementations
* Prototyped in Person.h
* Created on March 14, 2012, 6:59 PM
*/
#include <iostream>
#include <string>
#include "Person.h"
using namespace std;
//Constructor - initializes personName with string supplied as argument
Person::Person(string name)
{
setPerson (name);
} //End Person constructor
//Function - sets personName
void Person::setPerson(string name)
{
personName = name;
}
//Function - gets personName
string Person::getPerson()
{
return personName;
}
//Function - sets personAge
void Person::setAge(int age)
{
personAge = age;
}
//Function - gets personAge
int Person::getAge()
{
return personAge;
}
//Function - sets personHeight
void Person::setHeight(int height)
{
personHeight = height;
}
//Function - gets personHeight
int Person::getHeight ()
{
return personHeight;
}
//Function - gets the name of the oldest person
string Person::getOldest (Person pers[])
{
int high = 0;
for (int i = 0; i < 4; i++)
{
if (pers[i].getAge() > high)
high = i;
}
return pers[high].getPerson();
}
//Function - gets the name of the youngest person
string Person::getYoungest (Person pers[])
{
}
//Function - gets the name of the tallest person
string Person::getTallest (Person pers[])
{
}
//Function - gets the name of the shortest person
string Person::getShortest (Person pers[])
{
}
main.cpp
/*
* File: main.cpp
* Main Program of CarnathanHW1
*
* Created on March 14, 2012, 6:49 PM
*/
#include <cstdlib>
#include <iostream>
#include "Person.h"
using namespace std;
//Function - main: Program Execution
int main() {
//create Person objects in an array
Person personArray[] =
{
Person("Adam"), Person("Beth"), Person("Carl"), Person ("Didi")
};
for (int i = 0; i < 4; i++)
{
int tempAge = 0;
int tempHeight = 0;
cout << "Enter the age of " << personArray[i].getPerson() << ": ";
cin >> tempAge;
personArray[i].setAge(tempAge);
cout << "Enter the height of " << personArray[i].getPerson()
<< " (integer inches): ";
cin >> tempHeight;
personArray[i].setHeight(tempHeight);
cout << endl;
}
for (int i = 0; i < 4; i++)
{
cout << personArray[i].getPerson() << " " << personArray[i].getAge()
<< " " << personArray[i].getHeight() << endl << endl;
}
cout << "The oldest person is: " << personArray.getOldest(personArray);
return 0;
} //End main
So my problem is I receive this error "main.cpp:45 error: request for member 'getOldest' in 'personArray', which is of non-class type 'Person[4]'"
and occurs at this line:
cout << "The oldest person is: " << personArray.getOldest(personArray);
I know that I'm not calling the function correctly, I just don't know how to do it.
getOldest(personArray) doesn't work either.
Please help me solve this problem. Thanks.