0

I am developing an application in C++. In this app, I have two classes: database class and user class. Database class contains a list of pointers to users. In this class, I need to access to user's pointer to save information about users in an external file. In user class, I have all user's information.

Now, I would like to access to the database from user class, because I need scan it. My problem is this: Database class include user's class and when I include database from user's class I get a circular dependency. What can I do?

Forward declaration doesn't help me, cause I need to use methods both from user and from database.

Here a code example:

#include "user.h"

class Database{
private:
    std::map<string,User*> userList;
public:
    ....
    methods to use database
    ....
};


#include "database.h" // if I add this I get cirular-dep

class User{
private:
    ...user informations..
public:
    ...
    I need a method here to access to db
    ...
};
Sneftel
  • 40,271
  • 12
  • 71
  • 104
KiraLive
  • 23
  • 5
  • Do you have your declarations in header files and implementation in `.cpp` files? – Biffen Mar 23 '15 at 10:36
  • _"Forward declaration doesn't help me, cause I need to use methods both from user and from database"_ Do you really need to implement those uses inline with the declaration? I have never seen a case where this wasn't solvable using forward declaration. – πάντα ῥεῖ Mar 23 '15 at 10:37
  • It can't help me. I need to use user's method from database, and from user's I need to use database methods. Forward declaration it's useless in this case. – KiraLive Mar 23 '15 at 10:51

0 Answers0