-1

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

Lapsio
  • 6,384
  • 4
  • 20
  • 26
  • 2
    So stop defining those functions lexically inline. Contrary to your claims, this has been solved many times. – Lightness Races in Orbit Mar 30 '15 at 19:31
  • 2
    [Rule of Three](http://stackoverflow.com/questions/4172722/what-is-the-rule-of-three). – Casey Mar 30 '15 at 19:31
  • 1
    You should not reinvent std::shared_ptr (If you really want to do it, grab some sources and adjust these to your needs) –  Mar 30 '15 at 19:32

1 Answers1

3

Would it be wrong to put ~GCRefLink destructor implementation after the second class (ideally in a separate .cpp file)?

class GCRef;

class GCRefLink {
    friend class GCRef;
private:
    GCRef * ref;
    GCRefLink(GCRef * ref) : ref(ref) {}
public:
    ~GCRefLink();
};

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); }
};

GCRefLink::~GCRefLink(){ this->ref->unlink(); }
AlexD
  • 32,156
  • 3
  • 71
  • 65