#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    14
    Rep Power
    0

    Problem with friendship


    Hi, I've got a strange conundrum with friendship. I'm trying to grant a membership function of one class friendship of another.

    Code:
    class Cluster 
    {	friend Cluster Union::merge(Cluster *A, Cluster *B);
    public:
    	Cluster();
    	Cluster initCluster(Cluster* L, int N){LEADER = L; NODES.push_back(N);}
    	Cluster *getLeader(){return LEADER;}
    	vector <int> getCluster(){return NODES;}
    private:
    	Cluster *LEADER;
    	vector <int> NODES;
    };
    
    class Union
    {
    public:
    	void merge(Cluster *A, Cluster *B);
    	int getSpacing(){return SPACING;}
    	Union initUnion();
    private:
    	vector <Cluster> UNION;
    	int SPACING;
    };
    
    void Union::merge(Cluster *A, Cluster *B)
    {
    	A->	NODES;
    
    }
    However, there is a problem - when the IDE comes to the declaration of the friendship, it does not recognise what 'Union' is, and comes up with the error that Union is undefined - meanwhile placing the declaration of the Cluster class before the Union class would generate even more problems :( . I have tried adding 'class Union;' before the declaration of the Cluster class, but that does not work either. So what is the correct way of doing this, short of just declaring one class to be a friend of another?
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    14
    Rep Power
    0
    For anyone encountering the same problem as above, it turns out that we only need to switch the classes and add a forward declaration. Then there is no more circular dependency.

IMN logo majestic logo threadwatch logo seochat tools logo