0

我定义了以下基类:

class CDeviceClientRequest
{
public:
   /** Enumeration for the client message identifier. */
   enum EClientMessageIdentifier
   {
      TS_SET_ACTIVE,      // Set Active request for Total Station device.
      GPS_SET_ACTIVE,     // Set Active request for GPS device.
      TS_SET_COM_PORT,    // Set COM Port request for Total Station device.
      GPS_SET_COM_PORT,   // Set COM Port request for GPS device.
      TS_GET_OBSERVATION, // Get Observation request for Total Station device.
      GPS_GET_POSITION,   // Get Position request for GPS device.
      GPS_GET_QUALITY     // Get Quality request for GPS device.
   }; // EClientMessageIdentifier

   /**
    * Getter method for the client message identifier.
    */
   EClientMessageIdentifier getMessageId();

protected:
   /**
    * Protected constructor.
    *
    * @param messageId
    *           The enumeration representing the request message identifier.
    */
   CDeviceClientRequest(EClientMessageIdentifier messageId) 
       : m_iMessageId(messageId) {}

private:
   /** The enumeration representing the request message identifier. */
   EClientMessageIdentifier m_iMessageId;
}; // CDeviceClientRequest

这有以下子类:

class CDeviceSetActiveRequest : CDeviceClientRequest
{
public:
   /**
    * Getter method for the activation flag.
    */
   bool getActiveFlag();

protected:
   /**
    * Protected constructor.
    *
    * @param messageId
    *           The enumeration representing the request message identifier.
    * @param setActive
    *           The flag for the activation request.
    */
   CDeviceSetActiveRequest(EClientMessageIdentifier messageId,
                           bool                     setActive)
       : CDeviceClientRequest(messageId), 
         m_bActive(setActive) {}

private:
   /** Flag for the activation request. */
   bool m_bActive;
}; // CDeviceSetActiveRequest

它还具有以下可实例化的子类:

class CDeviceTSSetActiveRequest : CDeviceSetActiveRequest
{
public:
   /**
    * Public constructor.
    *
    * @param setActive
    *           The flag for the activation request.
    */
   CDeviceTSSetActiveRequest(bool setActive) 
       : CDeviceSetActiveRequest(CDeviceClientRequest::TS_SET_ACTIVE, 
                                 setActive) {}
}; // CDeviceTSSetActiveRequest

所以你创建了一个类型为 CDeviceTSSetActiveRequest 的对象,传入一个布尔参数。它调用超类 CDeviceSetActiveRequest 构造函数,传入请求类型的枚举值。它最后调用 CDeviceClientRequest 设置私有成员变量的枚举值。

但是代码拒绝编译并给出以下错误消息:

camd011> make
g++ -c CDeviceTSSetActiveRequest.cpp \
        -I.. -o bin/CDeviceTSSetActiveRequest.o
In file included from CDeviceTSSetActiveRequest.cpp:13:0:
CDeviceClientRequest.h: In constructor 'device::request::CDeviceTSSetActiveRequest::CDeviceTSSetActiveRequest(bool)':
CDeviceClientRequest.h:30:1: error: 'class device::request::CDeviceClientRequest device::request::CDeviceClientRequest::CDeviceClientRequest' is inaccessible
CDeviceTSSetActiveRequest.h:37:34: error: within this context
make: *** [bin/CDeviceTSSetActiveRequest.o] Error 1

任何人都可以帮忙吗?如何在可实例化的子类中使用主基类中定义的枚举?

忘了补充:文件的 cpp 版本实现了 getter 方法。

  • CDeviceClientRequest::getMessageId() 在 CDeviceClientRequest.cpp
  • CDeviceSetActiveRequest.cpp 中的 CDeviceSetActiveRequest::getActivFlag()
  • CDeviceTSSetActiveRequest.cpp 为空,仅包含标头。

以上2个cpp文件,用getter方法,编译没有问题。它只是发生错误的第 3 次。

4

2 回答 2

1

您忘记使用公共继承,如下所示:

class CDeviceSetActiveRequest : public CDeviceClientRequest
/* ... */
class CDeviceTSSetActiveRequest : public CDeviceSetActiveRequest
于 2013-07-03T17:14:54.203 回答
0

问题是你私下继承你的类,所以中间类隐藏了枚举。

您可能希望在这里公开继承:class CDeviceSetActiveRequest : public CDeviceClientRequest并且class CDeviceTSSetActiveRequest : public CDeviceSetActiveRequest(请注意我在其中添加了公共一词)。

于 2013-07-03T17:16:08.447 回答