friend functions to make use of cin and cout with objects, need some help

i need to make 2 friend functions to make use of cin and cout with objects.  When I compile this programi get errors saying i dont have access to private members..   I thought friend functions could access them.



class voter
{

friend ifstream& operator>> (ifstream&, voter v1);
friend ostream& operator << (ostream&, const voter v1);
      
private:      
      int  idnumber;
      char lastname [25];
      char firstname[25];
      char party         [2];
      int  birthyear;
      int  currentage;

public:
      voter();
      voter(int,char[], char[], char[], int, int);
      voter(const voter&);
      void Add();
      void Edit();
      void operator = (const voter&);
      void print(int);
      void disksave(ofstream& outfile);
      ~voter();

};

voter::voter()
{
      idnumber       =   0;
      lastname [0]   = '\0';
      firstname[0]   = '\0';
      party    [0]   = '\0';
      birthyear      =   0;
      currentage     =   0;
}

voter::voter(int id,char ln[], char fn[], char pid[], int year, int by)
{
      idnumber   = id;
      strcpy(lastname, ln);
      strcpy(firstname, fn);
      strcpy(party, pid);
      birthyear  = year;
      currentage = 2003 - birthyear;
}

voter::voter(const voter& v1)
{
      idnumber   = v1.idnumber;
      strcpy(lastname, v1.lastname);
      strcpy(firstname, v1.firstname);
      strcpy(party, v1.party);
      birthyear  = v1.birthyear;
      currentage = 2003 - v1.birthyear;
}

void voter::operator = (const voter& v1)
{
      idnumber   = v1.idnumber;
      strcpy(lastname, v1.lastname);
      strcpy(firstname, v1.firstname);
      strcpy(party, v1.party);
      birthyear  = v1.birthyear;
      currentage = 2003 - v1.birthyear;
}

void voter::Add()
{

      cls();
      int age =0;

      char trash[2];
      
      cout<<"Enter your voter id number: ";
      cin >> idnumber;

      while ((idnumber < 1000) || // Loop that validates voter
              ( idnumber > 10000))  // id number.

      {      
            cls();
            cout << "Valid ID number is between 1000 and 10000"
                   << endl << endl;

            cout << "Please enter a valid voter id number > ";
            cin  >>  idnumber;
            cout << endl;
      }

      cout<<"Enter your last name: ";
      cin.getline(trash,2);
      cin.getline(lastname, 25);

      cout<<"Enter your first name: ";
      cin.getline(firstname, 25);

      cout<<"Enter your party id letter: ";
      cin>>party;

      cout<<"Enter your birth year: ";
      cin>>birthyear;

      while((birthyear < 1878) || (birthyear > 1985))
      {
      
            cls();
            age = 2003 - birthyear;
      
      
            cout << " You must be atleast 18 yrs. old to vote!!"
                   << endl;
            cout << " Your current age: " << age << endl;



            cout << "\n Please enter a valid birth year: ";
            cin  >> birthyear;
      }

      currentage = 2003 - birthyear;      
}

void voter::Edit()
{
      cout<<"What is the voters new Party: ";
      cin>>party;

}

void voter::print(int label)
{
      cout << setw(3)
             << label            << setw(10)
           << idnumber      << setw(13)
             << lastname      << setw(8)  
             << firstname      << setw(9)
             << party            << setw(10)
             << birthyear      << setw(8)
             << currentage  << setw(8)
             << endl;
}

void voter::disksave(ofstream& outfile)
{
outfile  << setw(6)  << idnumber
             << setw(14) << lastname  
             << setw(12) << firstname
             << setw(12) << party
             << setw(12) << birthyear
             << setw(12) << currentage  
             << setw(12)
         << endl;
}


voter::~voter()
{



}



ifstream& operator >> (ifstream& vin, voter& v1)
{
      cls();
      int age =0;

      
      cout<<"Enter your voter id number: ";
      cin >> idnumber;


      cout<<"Enter your last name: ";
      cin.getline(trash,2);
      cin.getline(lastname, 25);

      cout<<"Enter your first name: ";
      cin.getline(firstname, 25);

      cout<<"Enter your party id letter: ";
      cin>>party;

      cout<<"Enter your birth year: ";
      cin>>birthyear;


      currentage = 2003 - birthyear;      

      return vin;


}

ostream& operator << (ostream& vout, const voter& v1)
{

vout    << setw(6)  << v1.idnumber
             << setw(14) << v1.lastname  
             << setw(12) << v1.firstname
             << setw(12) << v1.party
             << setw(12) << v1.birthyear
             << setw(12) << v1.currentage  
             << setw(12)
         << endl;

return vout;

}




namenAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

blahpersCommented:
Your declarations for operator<< and operator>> look like:

friend ifstream& operator>> (ifstream&, voter v1);
friend ostream& operator << (ostream&, const voter v1);

But your definitions look like:

ifstream& operator >> (ifstream& vin, voter& v1)
ostream& operator << (ostream& vout, const voter& v1)

The declarations take voter as arguments; the definitions take reference-to-voter.  Just stick the missing ampersands in your declarations so they match the definitions.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mayank SAssociate Director - Product EngineeringCommented:
Also, in your ifstream .... >> () definition:

>>  cin >> idnumber;

should be: cin >> v1.idnumber;

You cannot access data-members directly without using the objects because this is not a member function but a friend function. Use v1.lastname instead of lastname, and so on.
0
JensUniwebCommented:
Is friend functions realy able to access private. Isn't it only protected data that is available???

I remember it as private is alwas only available to local class. But protected is available to friend and inheritaged.
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Mayank SAssociate Director - Product EngineeringCommented:
No, friends can access private data. That rule applies to inheritance. While inheriting from a class, you can only access the protected members of the base class. However, the private members are also inherited but not directly accessible. They can be accessed by get () and set () methods in the base-class.

(Its been years since I worked on C++.... I'm working on J2EE now, but I hope that I remember it right.)
0
sin_Commented:
Did you find your answer? Guys have given you the answers. If you haven't happend to look at the above, please go through a few suggestions:

1. Always pass by reference
friend ifstream& operator>> (ifstream&, voter v1);
friend ostream& operator << (ostream&, const voter v1);

You are passing by value here. It means that a copy ctor will be called when you pass it. Had you used the reference, you would have avoided this costly operation.

2. It's very much true that friend functions or classes can access the private data members of the other classes.
But you need to access the data members through the object names. Otherwise, they are simply undeclared variables..rite?
ifstream& operator >> (ifstream& vin, voter& v1)
{
     cls();
     int age =0;

     
     cout<<"Enter your voter id number: ";
     cin >> idnumber;


it should be, cin >> v1.idnumber


hope it helps
-sin
0
namenAuthor Commented:
Thanks for all the help i have it working now.
0
Mayank SAssociate Director - Product EngineeringCommented:
With all due respect, I think that you should've split between blahpers and mayankeagle.
0
blahpersCommented:
I agree.  I only gave the program a once-over; I wasn't expecting my answer to solve everything, and mayankeagle showed that it didn't.
0
Mayank SAssociate Director - Product EngineeringCommented:
Anyways, I don't mind. I guess namen is also very new to EE and doesn't know about splitting points.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.