samedi 19 janvier 2019

C++ - class dependency - circular problem [duplicate]

Could you give me advice how should I properly structure my code? I am trying to sort students, and to do it I need access to Database object and also private member of Student - index. Now I get error:

error: ‘Database’ has not been declared friend void sortStudents(Database db);

but If I reorder classes I will get:

Student’ was not declared in this scope std::vector data;

What is the proper solution?

class Student
{
    private:
        int _index {0};
    public:
        friend void sortStudents(Database db);
};

class Database
{
    private:
        std::vector <Student> data;
    public:
        friend void sortStudents(Database db);
};


void sortStudents(Database db)
{
    std::sort((db.data).begin(), (db.data).end(), 
             [](const Student& leftSide, const Student& rightSide)
             { return leftSide._index < rightSide._index; }
             );
}

Aucun commentaire:

Enregistrer un commentaire