开发者

c++ forward declaration of pure virtual class

I have a forward deceleration problem. I had a normal class before, called GlobalCWND, it was instantiated and used in another class ProtocolContext.

I forward declare the ProtocolContext class in Requestor.h.

You can see the related part of the code of this 2 classes.

============Protocol Context========

 class Requestor<RequestorStrategy>;
 class Receiver<ReceiverStrategy>;

 class ProtocolContext : public Object
 {
 public:
   ProtocolContext(Ptr<SubscriptionContext> , Ptr<ReceiverStrategy> , Ptr<
       RequestorStrategy> );
.......

 ProtocolContext::ProtocolContext(Ptr<SubscriptionContext> sctx, Ptr<
      ReceiverStrategy> rec, Ptr<RequestorStrategy> req) :
    receiver(rec), requestor(req)
  {
    m_sctx = sctx;
    Ptr<GlobalCWND> m_globalCWND = Create<GlobalCWND> (this);
    cout << "Initializing Protocol Context for rid"
        << m_sctx->GetMetaDataStrategy()->GetRid() << endl;
    m_ph = Create<PacketHandler> (sctx);
  }

=================Requestor.h=========

 class ProtocolContext;

 class RequestorStrategy : public Object
 {

 public:
   RequestorStrategy()
   {
   }
   ;
   ~RequestorStrategy()
   {
   }
   ;
   /* Transitions */
   virtual void
   Trans(Ptr<ProtocolContext> , Ptr<Packet> ) = 0;
 };

.....

It worked properly before. Now I have change the GlobalCWND to a virtual class and In the protocol Context constructor I have this code:

ProtocolContext::ProtocolContext(Ptr<SubscriptionContext> sctx, Ptr<
      ReceiverStrategy> rec, Ptr<RequestorStrategy> req) :
    receiver(rec), requestor(req)
  {
    m_sctx = sctx;
    Ptr<GlobalCWND> m_globalCWND = Create<GlobalCWNDSimple> (this);

..............
}

But now I get 开发者_高级运维this error:

 GlobalCWND.h:21:   instantiated from here
ptr.h:441: error: invalid use of incomplete type ‘struct
ProtocolContext’
Requestor.h:16: error: forward declaration
of ‘struct ProtocolContext’

the error lines are:

line 21 of GlobalCWND() is    GlobalCWND (){};
and line 16 of Requestor.h is class ProtocolContext;

Ptr class is actually creating pointer to the object, line 441 of ptr.h is the last line of this function:

template <typename T>
Ptr<T>::~Ptr ()
{
 if (m_ptr != 0)
   {
     m_ptr->Unref();
   }
}

The code for GlobalCWND starts like this:

 class ProtocolContext;

  class GlobalCWND : public Object
  {
  public:
    GlobalCWND (){};
    GlobalCWND (Ptr<ProtocolContext>){};
    ~GlobalCWND (){};

    static TypeId GetTypeId(){
      static TypeId tid = TypeId("GlobalCWND")
      .SetParent<Object> ()
      ;
      return tid;
    };


The constructor for GlobalCWND takes a Ptr<ProtocolContext> by value, which in its destructor calls a method on the ProtocolContext pointer.
According to the error however, ProtocolContext is incomplete at that point - you need to include the declaration for ProtocolContext.
For a list on what you can and can't do with incomplete types, see this answer.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜