我有向前减速的问题。我以前有一个普通的类,称为GlobalCWND,它已实例化并用于另一个类ProtocolContext

我在Requestor.h中向前声明ProtocolContext类。

您可以看到这两个类的代码的相关部分。

============协议上下文========

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

.....


之前它工作正常。现在,我已将GlobalCWND更改为虚拟类,并且在协议上下文构造函数中,我具有以下代码:

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

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


但是现在我得到了这个错误:

 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’


错误行是:

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


Ptr类实际上是在创建指向该对象的指针,ptr.h的第441行是此函数的最后一行:

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


GlobalCWND的代码如下所示:

 class ProtocolContext;

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

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

最佳答案

GlobalCWND的构造函数采用值形式的Ptr<ProtocolContext>,该构造函数在其析构函数中调用ProtocolContext指针上的方法。
但是,根据错误,ProtocolContext当时是不完整的-您需要包括ProtocolContext的声明。
有关不完整类型可以做什么和不能做什么的列表,请参见this answer

09-26 14:48