lundi 30 mars 2015

c++ - incomplete type / forward declaration

This question has been asked many times but usually it could be easily solved by changing order of classes. In my case it may be not.



class GCRefLink;
class GCRef;

class GCRefLink {
friend class GCRef;
private:
GCRef * ref;
GCRefLink(GCRef * ref) : ref(ref) {}
public:
~GCRefLink(){this->ref->unlink();}
};

class GCRef {
friend class GCRefLink;
private:
int refCount;

GCRef() : refCount(0) {}
virtual ~GCRef(){}

void unlink(){--this->refCount;if(this->refCount==0) delete this;}
public:
GCRefLink link(){++this->refCount;return GCRefLink(this);}
};


When I change order of classes I'm getting the same error just about second class. It's meant to be reference class to inherit by managed, undeleteable classes, I know there's already such a thing in stl but as it's university project I need to implement my own.


I'm getting invalid use of incomplete type 'class GCRef' or invalid use of incomplete type 'class GCRefLink' errors


Aucun commentaire:

Enregistrer un commentaire