2

我正在做我的项目,即模拟学校系统。
我对来自 Klasa 类(类波兰语)的方法无法访问 Student 类的私有数据这一事实有疑问,即使我在 Student 中将此方法声明为朋友。

这是代码:

第一个学生.h:

#include <iostream>
#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&);
        Student& operator= (const Student&);
        ~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&);
        bool operator!= (const Student&);
        friend ostream& operator<< (ostream&, const Student&);
        // akcesory
        int GetclassIndex() const;
        int SetclassIndex(int i);
        friend bool Klasa::assignToKlasa(Student* s);
        char* GetName() const { return name; }
    };

现在来自 Klasa 类的方法:

    bool Klasa::assignToKlasa(Student* s)
    {
        if(studentCount < maxStudentCount)
        {
            if(containsStudent(s))
                return false;

            if(studentCount < klasaSize) //jest miejsce dla studenta
            {
                int i = 0;
                while(studentList[i] != NULL)
                    i++;
                studentList[i] = s;

                s->classIndex=i;// = i; //tu musi byc setter
            }
            //WHATS BELOW DOESN'T MATTER FOR THIS PROBLEM, SO U DON;T NEED TO READ.


            else
            {
                cout << "Zwiekszamy rozmiar!\n";
                //musimy zwiększyć rozmiar tablicy studentList
                Student** newStudentList = new Student*[klasaSize + sizeStep];
                for(int i = 0; i < klasaSize; ++i)
                    newStudentList[i] = studentList[i];

                for(int i = klasaSize; i < klasaSize + sizeStep; ++i)
                    newStudentList[i] = NULL;

                klasaSize += sizeStep;

                delete[] studentList;
                studentList = newStudentList;

                studentList[studentCount] = s;

                s->SetclassIndex(studentCount); //tu ma byc setter

            }

            s->assignToClass(this); //mówimy studentowi - "jestem twoją klasą"
            studentCount++;
            cout << "Dodano studenta!\n";

            return true;
        }

        return false;
    }

它强调s->classIndex=i无法访问 classIndex。

同样在 Klasa.h 我声明了这个方法,如:bool assignToKlasa(Student* s);

所以我不知道为什么在我将学生声明为朋友之后它不允许我访问学生的私人数据?请你帮助我好吗?

4

2 回答 2

4

Your definition of class Student { ... } shouldn't compile either. The friend declaration is an error, so I'm surprised that isn't highlighted too. What your IDE has figured out is that the access to a private member isn't allowed without a working friend declaration.

The error is that a forward declaration of Klasa is not enough to declare a member function of Klasa to be a friend of Student. You need the class definition of Klasa first.

So, it should be:

class Student; // forward declaration, because members of Klasa take Student*

class Klasa { 
    ... 
};

class Student { 
    ... 
};

then you can define Klasa::assignToKlasa.

于 2012-06-13T17:33:52.547 回答
1

对于这个等效代码

class A;
class B {
  int x;
public:
  B(int __x) : x(__x) {}
  friend bool A::method(B*) const;   // error: undefined member cannot be friend
//friend class A;                    // fine:  class A is declared and can be used
};

class A {
  int y;
pu8blic:
  A(int __y) : y(__y) {}
  bool method(B*b) const
  { return y == b->x; }
};

我收到错误消息(使用 gcc 4.3.2):

test3.cc:7: 错误:不完整类型'struct A'的无效使用</p>

test3.cc:1:错误:“结构 A”的前向声明</p>

test3.cc:在成员函数'bool A::method(B*) const'中:

test3.cc:3:错误:'int B::x' 是私有的

test3.cc:15:错误:在此上下文中

第一个错误是您唯一需要担心的错误(其他错误是第一个错误的解释或后果)。那么如何解决这个问题呢?解决问题的一种简单方法是通过替换声明整体class A成为朋友B

  friend bool A::method(B*) const;

  friend class A;

这是定义的。

于 2012-06-13T17:36:46.213 回答