lundi 5 février 2018

c++ overloaded friend function can't access private members

currently doing a project where my professor requires us to overload the stream extraction and input operators. I copied the header he gave us to use as an implementing it as I am supposed to as far as what I can tell from an hour of looking online. Here's my header student.h

// @file student.h
#ifndef STUDENT_H
#define STUDENT_H
#include <string>
using namespace std;
class Student {
    /** add all the setter and getter methods **/
    /**
   * @param is the input stream
   * @param course the student object reference
   * @return the input stream
   */
    friend istream &operator >> (istream &is, Student &student);
    /**
   * @param os the output stream
   * @param course the student object reference
   * @return the output stream
   */
    friend ostream& Student::operator << (ostream& os, const Student& student);

public:
    Student();
    Student(string firstName, string lastName, int id, char grade);

    void setFirstName(string firstName);
    string getFirstName();


private:
    string firstName;
    string lastName;
    int id;
    char grade;
};
#endif /* STUDENT_H */

And here is the definition I'm using the the file student.cpp

#include "student.h"
#include <iostream>;
using namespace std;


istream &operator >> (istream &is, Student &student) {
    is >> student.firstName;
}

CLion keeps telling me that firstName is private and therefore inaccessible, is there anything obvious I'm missing? I've checked and double checked my formatting and moved the ampersands around a lot and I'm having a lot of trouble telling what it is.

And yes, I've already looked at the similarly titled question where he was having issues with the namespaces he was using, tried it and saw no results. Any help is greatly appreciated.

Aucun commentaire:

Enregistrer un commentaire