title: C++ Friend method cannot access private data from class
body: <p>I am doing my project which is simulation of school system.<br>
  I have problem with the fact that a method from class Klasa (Polish for class) cannot access private data from class Student, even though I declared this method as a friend in Student.</p>
  
  <p>Here is the code:</p>
  
  <p>First Student.h:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include "Klasa.h"
  #include "SubjectType.h"
  #include "SubjectGrades.h"
  using namespace std;
      class Klasa;
      class SubjectGrades;
  
      class Student
      {
      private:
          char* name;
          int id;
  
  
          Klasa* myClass;
          //School* mySchool;
          int classIndex; //numer w dzienniku - zrobić prywatne z akcesorami
          static int counter;
      public:
  
  
  
          Student();
          Student(char*);
          Student(const Student&amp;);
          Student&amp; operator= (const Student&amp;);
          ~Student();
  
          //bool assignToSchool(School*)
          //bool resignFromSchool();
          void assignToClass(Klasa*);
          void resignFromClass();
          double getMyAverageGrade(SubjectType type) const ;      //srednia ocen z danego przedmiotu dla danego ucznia
          double getMyAverageGrade() const;       //średnia ocen dla danego ucznia
  
          bool operator== (const Student&amp;);
          bool operator!= (const Student&amp;);
          friend ostream&amp; operator&lt;&lt; (ostream&amp;, const Student&amp;);
          // akcesory
          int GetclassIndex() const;
          int SetclassIndex(int i);
          friend bool Klasa::assignToKlasa(Student* s);
          char* GetName() const { return name; }
      };
  </code></pre>
  
  <p>Now method from class Klasa:</p>
  
  <pre><code>    bool Klasa::assignToKlasa(Student* s)
      {
          if(studentCount &lt; maxStudentCount)
          {
              if(containsStudent(s))
                  return false;
  
              if(studentCount &lt; klasaSize) //jest miejsce dla studenta
              {
                  int i = 0;
                  while(studentList[i] != NULL)
                      i++;
                  studentList[i] = s;
  
                  s-&gt;classIndex=i;// = i; //tu musi byc setter
              }
              //WHATS BELOW DOESN'T MATTER FOR THIS PROBLEM, SO U DON;T NEED TO READ.
  
  
              else
              {
                  cout &lt;&lt; "Zwiekszamy rozmiar!\n";
                  //musimy zwiększyć rozmiar tablicy studentList
                  Student** newStudentList = new Student*[klasaSize + sizeStep];
                  for(int i = 0; i &lt; klasaSize; ++i)
                      newStudentList[i] = studentList[i];
  
                  for(int i = klasaSize; i &lt; klasaSize + sizeStep; ++i)
                      newStudentList[i] = NULL;
  
                  klasaSize += sizeStep;
  
                  delete[] studentList;
                  studentList = newStudentList;
  
                  studentList[studentCount] = s;
  
                  s-&gt;SetclassIndex(studentCount); //tu ma byc setter
  
              }
  
              s-&gt;assignToClass(this); //mówimy studentowi - "jestem twoją klasą"
              studentCount++;
              cout &lt;&lt; "Dodano studenta!\n";
  
              return true;
          }
  
          return false;
      }
  </code></pre>
  
  <p>It underlines <code>s-&gt;classIndex=i</code> that classIndex cannot be accessed.</p>
  
  <p>Also in Klasa.h I declared this method like: <code>bool assignToKlasa(Student* s);</code></p>
  
  <p>So I have no idea why it doesn't let me access private data from student after I declared it in student as a friend? Could you please help me?</p>