I have overloaded the << & >> operators and I though I could access private data members of the class if I declared the functions friends of the class. I am getting four errors in the two overloaded functions that are trying to access the private members firstName and lastName. Anyone have any ideas as to what I am doing wrong?
#ifndef H_personType
#define H_personType
#include <string>
using namespace std;
class personType
{
friend ostream& operator<< (ostream&, const personType &);
friend istream& operator>> (istream&, personType &);
public:
void print() const;
//Function to output the first name and last name
//in the form firstName lastName.
void setName(string first, string last);
//Function to set firstName and lastName according
//to the parameters.
//Postcondition: firstName = first; lastName = last
string getFirstName() const;
//Function to return the first name.
//Postcondition: The value of the firstName is returned.
string getLastName() const;
//Function to return the last name.
//Postcondition: The value of the lastName is returned.
personType(string first = "", string last = "");
//constructor
//Sets firstName and lastName according to the parameters.
//The default values of the parameters are empty strings.
//Postcondition: firstName = first; lastName = last
private:
string firstName; //variable to store the first name
string lastName; //variable to store the last name
};
#endif
#include <iostream>
#include <string>
#include "personType.h"
using namespace std;
ostream& operator<< (ostream& osObject, const personType& name)
{
osObject << name.firstName << name.lastName;
return osObject;
}
istream& operator>> (istream& isObject, personType& name)
{
isObject >> name.firstName >> name.lastName;
return isObject;
}
void personType::print() const
{
cout << firstName << " " << lastName << endl;
}
void personType::setName(string first, string last)
{
firstName = first;
lastName = last;
}
string personType::getFirstName() const
{
return firstName;
}
string personType::getLastName() const
{
return lastName;
}
//constructor
personType::personType(string first, string last)
{
firstName = first;
lastName = last;
}